<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn  Blog:http://blog.sina.com.cn/saverneo    |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\AuctionOrder;

use AuctionCore\Account\Back\Employee;
use AuctionCore\CarDealer\Dealer;
use AuctionCore\CarResource\CarModel;
use AuctionCore\Other\Address;
use AuctionCore\ChangeOrder\ChangeOrder;

/**
 * 拍单列表
 *
 * @author  Eric Lee
 */
class AuOrderList
{

    /**
     * 获取拍单列表
     *
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     *
     * @return mixed array(list,total)
     */
    public function get_list($page = 1, $size = 10, $paramer)
    {
        $map = array();
        //场次id
        if ($paramer['scene_id']) {
            $map['o.scene_id'] = array('eq', $paramer['scene_id']);
        }

        //拍单状态
        if ($paramer['order_status']) {
            $s = $paramer['order_status'] > 2 ? "egt" : "eq";
            $map['o.status'] = array($s, $paramer['order_status']);
        }
        //起拍时间
        if ($paramer['bidding_start_time']) {
            $map['o.bidding_start_time'] = array('like', $paramer['bidding_start_time'] . '%');
        }
        //关键字(拍卖单号，等级)
//        if (!empty($paramer['keyword'])) {
//
//            $where = array();
//            $where['o.order_no'] = array('eq', urldecode($paramer['keyword']));
//            $where['c.composite_state'] = array('eq', urldecode($paramer['keyword']));
//            $where['c.accident_level'] = array('eq', urldecode($paramer['keyword']));
//            $where['_logic'] = 'or';
//            $map['_complex'] = $where;
//        }
        //关键字(车辆编号，等级)
        if (!empty($paramer['keyword'])) {
            $where = array();
            $keyword = trim($paramer['keyword']);
            if (ctype_alpha($keyword) || ctype_digit($keyword)) {         //如果为纯数字或纯字符则模糊搜索
                $where['o.order_no'] = array('LIKE', '%' . $keyword . '%');
                $where['c.composite_state'] = array('LIKE', '%' . $keyword . '%');
            } else {
                $keyword_first = substr($keyword, 0, 1);
                $keyword_front = substr($keyword, 0, -1);
                $keyword_last = substr($keyword, -1);
                if (ctype_alpha($keyword_first)) {    //首字母为字符判定为车辆编号
                    $where['o.order_no'] = array('LIKE', '%' . $keyword . '%');
                }
                if (ctype_alpha($keyword_last)) {       //尾字符为字母判定为车辆等级
//                    $where['c.composite_state'] = array('eq', $keyword_front);
//                    $where['c.accident_level'] = array('eq', strtoupper($keyword_last));
                    $where['_string'] = "c.composite_state='" . $keyword_front . "' and c.accident_level='" . strtoupper($keyword_last) . "'";
                }
            }
//            $where['c.car_no'] = array('eq', urldecode($paramer['keyword']));
//            $where['c.composite_state'] = array('eq', urldecode($paramer['keyword']));
//            $where['c.accident_level'] = array('eq', urldecode($paramer['keyword']));
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        //车辆所在地
        if (!empty($paramer['location_area'])) {
            $map['c.location_area'] = array('in', $paramer['location_area']);
        }
        $cols = "o.order_id,o.order_no,o.car_id,o.scene_id,o.status,o.coupon,o.transfer_fee,o.commision,
                    o.bid_start_price,o.est_elapsed_time,o.bidding_start_time,o.rank,
                    c.suggest_bid_start_price,c.pointer_mileage,c.emission,c.plate_prefix,c.plate_suffix,
                    c.car_no,c.brand_id,c.series_id,c.model_id,o.reserve_price,o.show_reserve_price,
                    c.composite_state,c.accident_level,c.location_area,c.first_reg_date";

        $total = M('order')->alias('o')->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")->where($map)->count();
        $list = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
            ->where($map)->limit($size)->page($page)->order("o.status")->select();

        //echo M()->getLastSql();

        $cm = new \AuctionCore\CarResource\CarModel();
        $a = new \AuctionCore\Other\Address();

        /*$aa = C('EMISSION')[2];
       // print_r($aa);
        return $aa;*/
        //数据处理
        foreach ($list as $key => $val) {
            $auOrder = new \AuctionCore\AuctionOrder\AuOrder($val['order_id']);
            $scene = $auOrder->get_scene($val['scene_id']);

            $list[$key]['scene_name'] = $scene['name']; //场次名称
            //所在地
            $list[$key]['location_area'] = $a->get_name_by_code($val['location_area']);
            //车型
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            $list[$key]['first_reg_date'] = date('Y年m月d日', strtotime($val['first_reg_date']));//初登日期
            $list[$key]['pointer_mileage'] = round($val['pointer_mileage'] / 10000, 2);//里程
            $plateAddress = C('PLATE_PREFIX')[$val['plate_prefix']];
            $suffix = substr($val['plate_suffix'], 0, 1);
            $list[$key]['plate'] = $plateAddress . $suffix; //车牌简称 如:京A
            $list[$key]['emission'] = C('EMISSION')[$val['emission']]; //车辆排放标准
            $list[$key]['reserve_price'] = format_money($val['reserve_price']);//保留价
            $list[$key]['bid_start_price'] = format_money($val['bid_start_price']);//起拍价
            $list[$key]['suggest_min_price'] = format_money($val['suggest_min_price']);//建议最低价
            $list[$key]['suggest_max_price'] = format_money($val['suggest_max_price']);//建议最高价
            $list[$key]['transfer_fee'] = format_money($val['transfer_fee']);//交易服务费
            $list[$key]['commision'] = format_money($val['commision']);//交付服务费

            //$list[$key]['reserve_price'] = format_money($val['reserve_price']);//起拍价

            $hour = date('H', strtotime($val['bidding_start_time']));
            $list[$key]['bidding_start_time'] = $hour . '点';
            $minute = date('i', strtotime($val['bidding_start_time']));
            if ($minute != '00') {
                $list[$key]['bidding_start_time'] .= $minute . '分';
            }
            $second = date('s', strtotime($val['bidding_start_time']));
            if ($second != '00') {
                $list[$key]['bidding_start_time'] .= $second . '秒';
            }
//          $list[$key]['est_elapsed_time'] = floor($val['est_elapsed_time']/60);
            $est_elapsed_time = $val['est_elapsed_time'];
            $list[$key]['est_elapsed_time'] = is_float($est_elapsed_time / 60) ? number_format($est_elapsed_time / 60, 1) : $est_elapsed_time / 60;
            $list[$key]['status_cn'] = C('ORDER_STATUS.' . $val['status']);
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 拍品查询 - 拍单列表
     *
     * @param integer $page
     * @param integer $size
     * @param array $params
     *
     * @return mixed array(list,total)
     */
    public function get_order_list($page = 1, $size = 10, $params)
    {
        $map = array();
        /**
         * 条件查询
         * 1、竞拍时间 2、拍单状态 3、来源 4、拍单编号 5、级别 6、车牌号
         */

        if ($params['time_mode'] == 2) { //过户完成时间
            if ($params['start_time']) {
                $map['tl.last_update'] = ['egt', $params['start_time'] . ' 00:00:00'];
            }
            if ($params['end_time']) {
                $map['tl.last_update'] = ['elt', $params['end_time'] . ' 23:59:59'];
            }
            if ($params['start_time'] && $params['end_time']) {
                $map['tl.last_update'] = [
                    ['egt', $params['start_time'] . ' 00:00:00'],
                    ['elt', $params['end_time'] . ' 23:59:59']
                ];
            }
        } else {
            if ($params['start_time']) {
                $map['o.bidding_start_time'] = ['egt', $params['start_time'] . ' 00:00:00'];
            }
            if ($params['end_time']) {
                $map['o.bidding_start_time'] = ['elt', $params['end_time'] . ' 23:59:59'];
            }
            if ($params['start_time'] && $params['end_time']) {
                $map['o.bidding_start_time'] = [
                    ['egt', $params['start_time'] . ' 00:00:00'],
                    ['elt', $params['end_time'] . ' 23:59:59']
                ];
            }
        }
        if ($params['order_status']) {
            $order_status = $params['order_status'];
            if ($order_status == 5) {  //待确认
                $map['o.status'] = ['eq', 5];
            } elseif ($order_status == 52) {      //确认失败
                $map['o.status'] = ['eq', 12];
                $map['o.fail_type'] = ['eq', 2];
            } elseif ($order_status == 6) {    //流拍重拍
                $map['o.status'] = ['eq', 12];
                $map['o.fail_type'] = [['eq', 1], ['eq', 6], 'OR'];  //待重拍,拍单状态为拍单失败,失败类型为车主要求重拍(流拍)
            } elseif ($order_status == 61) {       //磋商失败
                $map['o.is_consult'] = ['eq', 1];
                $map['con.consult_status'] = ['eq', 5];
            } elseif ($order_status == 7) {      //待签约
                $map['o.status'] = ['eq', 7];
                $map['o.return_check_status'] = [['eq', 5], ['eq', 7], 'OR'];
            } elseif ($order_status == 71) {   //签约失败
                $map['o.status'] = ['eq', 12];
                $map['o.fail_type'] = ['eq', 3];
            } elseif ($order_status == 87) {   //车商待争议
                $map['o.dealer_issue_status'] = ['eq', 1];
            } elseif ($order_status == 81) {   //待回车
                $map['crl.status'] = ['eq', 0];
                $map['o.status'] = ['eq', 8];
                $map['o.return_check_status'] = ['eq', 1];
            } elseif ($order_status == 82) {   //回车中
                $map['o.status'] = ['eq', 8];
                $map['o.return_check_status'] = ['eq', 2];
            } elseif ($order_status == 83) {   //待复检
                $map['o.status'] = [['eq', 7], ['eq', 8], 'OR'];
                $map['arc.status'] = [['eq', 0], ['eq', 1], 'OR'];
                $map['o.return_check_status'] = [['eq', 3], ['eq', 4], 'OR'];
            } elseif ($order_status == 84) {   //待验车
                $map['cdc.status'] = [['eq', 0], ['eq', 1], 'OR'];
                $map['o.check_car_status'] = ['eq', 1];
            } elseif ($order_status == 841) {   //已验车
                $map['c.pay_status'] = ['eq', 1];
                $map['o.status'] = ['eq', 8];
                $map['o.check_car_status'] = ['eq', 3];
            } elseif ($order_status == 849) {  //待结算
                $map['_string'] = '(o.status = 8 AND o.dealer_pay_status = 0 AND o.return_check_status in (5,6,7) AND c.car_source = 1) OR 
                                   (o.status = 8 AND o.dealer_pay_status = 0 AND c.car_source = 2)';
            } elseif ($order_status == 85) {   //待收款
                $map['o.status'] = ['eq', 8];
                $map['o.dealer_pay_status'] = [['eq', 1], ['eq', 2], 'OR'];
            } elseif ($order_status == 851) {   //已收款 -- 北京
                $map['c.delivery_mode'] = ['eq', 2];
                $map['o.status'] = ['eq', 8];
                $map['o.first_pay_status'] = ['eq', 0];
                $map['o.dealer_pay_status'] = ['eq', 3];
            } elseif ($order_status == 86) {   //车主待争议
                $map['oi.status'] = ['eq', 0];
            } elseif ($order_status == 861) {   //车主协商失败
                $map['oi.status'] = ['eq', 2];
            } elseif ($order_status == 871) {   //车商协商失败
                $map['o.status'] = ['eq', 12];
                $map['o.dealer_issue_status'] = [['eq', 3], ['eq', 4], 'OR'];
            } elseif ($order_status == 119) {    //平台确认失败
                $map['o.status'] = ['eq', 12];
                $map['o.fail_type'] = ['eq', 9];
            } elseif ($order_status == 12) {    //自收完成导致失败的拍单
                $map['o.status'] = ['eq', 12];
                $map['o.fail_type'] = ['eq', 10];
            } else {
                $map['o.status'] = ['eq', $order_status];
            }
        }
        if ($params['pay_status']) {
            $pay_status = $params['pay_status'];
            $map['c.pay_status'] = ['eq', $pay_status];
        }
        if ($params['comefrom'] != '-2') {
            $map['co.comefrom'] = array('eq', $params['comefrom']);
        }
        if ($params['order_no']) {
            $order_no = trim($params['order_no']);
            $map['o.order_no'] = array('LIKE', '%' . $order_no . '%');
        }
        if ($params['car_level']) {
            $car_level = trim($params['car_level']);
            $car_level_front = substr($car_level, 0, -1);
            $car_level_last = substr($car_level, -1);
            if (ctype_digit($car_level)) {
                $map['c.composite_state'] = array('LIKE', '%' . $car_level . '%');
            }
            if (ctype_alpha($car_level)) {
                $map['c.accident_level'] = array('LIKE', '%' . $car_level . '%');
            }
            if ($car_level_front && ctype_alpha($car_level_last)) {
                $map['_string'] = "c.composite_state LIKE '%" . $car_level_front . "%' and c.accident_level LIKE '%" . strtoupper($car_level_last) . "%'";
            }
        }
        if ($params['car_plate_number']) {
            $car_plate_number = trim($params['car_plate_number']);
            $car_plate_number_first = mb_substr($car_plate_number, 0, 1, 'UTF8');
            $car_plate_number_last = mb_substr($car_plate_number, 1, 7, 'UTF8');
            $plate_prefix = array_search($car_plate_number_first, C('PLATE_PREFIX'));
            $plate_suffix = substr($car_plate_number, 1);
            $map['c.plate_prefix'] = array('eq', $plate_prefix);
            if ($car_plate_number_last) {
                $map['c.plate_suffix'] = array('LIKE', '%' . $car_plate_number_last . '%');
            }
        }
        //$map['tl.result'] = array('eq',2);
        if ($params['location_area']) {
            $map['c.location_area'] = array('eq', $params['location_area']);
        }
        if ($params['bc_city']) {
            $where['c.location_area'] = ['in', $params['bc_city']];
            $map['_complex'] = $where;
            $map['_logic'] = 'AND';
        }

        $orderField = 'order_id,order_no,`status`,fail_type,bid_start_price,owner_confirm_service_id,dealer_pay_status,bid_best_price,bidding_best_price,reserve_price,show_reserve_price,bidding_start_time,return_check_status,check_car_status,first_pay_status,is_consult,is_lock,dealer_issue_status,car_id';
        $subQuery = M()->field($orderField)->table(C('DB_PREFIX') . "order")->order('order_id')->select(false);

        $cols = "c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.composite_state,c.accident_level
                ,c.car_status,c.plate_prefix,c.plate_suffix,c.location_area,c.pay_status,c.delivery_mode
                ,c.suggest_bid_start_price,c.car_source,c.tail_money,c.first_money,c.three_in_one,c.re_auction_type
                ,co.checker_id,co.comefrom,o.order_id
                ,o.order_no,o.status,o.fail_type,o.bid_start_price,o.owner_confirm_service_id,o.dealer_pay_status
                ,o.bid_best_price,o.bidding_best_price,o.reserve_price,o.show_reserve_price,o.bidding_start_time
                ,o.return_check_status,o.check_car_status,o.dealer_pay_status,o.first_pay_status,o.is_consult,o.is_lock
                ,o.dealer_issue_status,tl.last_update as transfer_success_time
                ,arc.status as arc_status,oi.status as oi_status,cdc.status as cdc_status,crl.status as crl_status";
        $order = "o.order_id DESC";
        //$total = M()->table($subQuery.' o')
        $total = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id` = c.`car_id`")
            ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id` = co.`owner_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "recheck as arc ON o.`order_id` = arc.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "owner_issue as oi ON o.`order_id` = oi.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "car_dealer_check as cdc ON o.`order_id` = cdc.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "car_return_log as crl ON o.`order_id` = crl.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "transfer_log as tl ON o.`order_id` = tl.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "consult as con ON o.`order_id` = con.`order_id`")
            ->where($map)->count();

        $list = M()->table($subQuery.' o')
        //$list = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id` = c.`car_id`")
            ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id` = co.`owner_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "recheck as arc ON o.`order_id` = arc.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "owner_issue as oi ON o.`order_id` = oi.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "car_dealer_check as cdc ON o.`order_id` = cdc.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "car_return_log as crl ON o.`order_id` = crl.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "transfer_log as tl ON o.`order_id` = tl.`order_id`")
            ->join("LEFT JOIN " . C('DB_PREFIX') . "consult as con ON o.`order_id` = con.`order_id`")
            ->field($cols)
            ->where($map)
            ->limit($size)
            ->page($page)
            ->order($order)
            ->select();

        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                $location_area = $addr->get_name_by_code($val['location_area']);
                $list[$key]['order_no'] = $val['order_no'];
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                $list[$key]['car_level'] = $val['composite_state'] . $val['accident_level'];
                $list[$key]['car_plate_number'] = C('PLATE_PREFIX')[$val['plate_prefix']] . $val['plate_suffix'];
                if ($val['bid_best_price'] >= $val['bidding_best_price']) {
                    $best_price = format_money($val['bid_best_price']);
                } else {
                    $best_price = format_money($val['bidding_best_price']);
                }
                $list[$key]['best_price'] = $best_price; //;format_money($val['bidding_best_price']);
                $list[$key]['bid_start_price'] = format_money($val['bid_start_price']);
                $list[$key]['reserve_price'] = format_money($val['reserve_price']);
                $list[$key]['bidding_start_time'] = date("Y年m月d日 H:i", strtotime($val['bidding_start_time']));
                $list[$key]['transfer_success_time'] = $val['transfer_success_time'] ? date("Y年m月d日 H:i", strtotime($val['transfer_success_time'])) : '--';
                $list[$key]['suggest_bid_start_price'] = format_money($val['suggest_bid_start_price']);
                $fail_type = $val['fail_type'];
                $status = $val['status'];
                if ($status == 5) { //待确认
                    $status_code = 5;
                } elseif ($status == 12 && $fail_type == 2) { //确认失败
                    $status_code = 52;
                } elseif ($status == 12 && ($fail_type == 1 || $fail_type == 6)) {
                    $status_code = 6;
                } elseif ($val['is_consult'] == 1 && $val['consult_status'] == 5) {
                    $status_code = 61;
                } elseif ($status == 7 && ($val['return_check_status'] == 5 || $val['return_check_status'] == 7)) {  //待签约
                    $status_code = 7;
                } elseif ($status == 12 && $fail_type == 3) {   //签约失败
                    $status_code = 71;
                } elseif ($val['dealer_issue_status'] == 1) {    //车商待争议
                    $status_code = 87;
                } elseif ($status == 12 && ($val['dealer_issue_status'] == 3 || $val['dealer_issue_status'] == 4)) {    //车商协商失败
                    $status_code = 871;
                } elseif ($val['is_lock'] == 1) {
                    $status_code = 862;
                } elseif ($val['oi_status'] == 2) {    //车主协商失败
                    $status_code = 861;
                } elseif ($status == 8 && $val['return_check_status'] == 1 && $val['crl_status'] == 0) { //待回车
                    $status_code = 81;
                } elseif ($status == 8 && $val['return_check_status'] == 2) { //回车中
                    $status_code = 82;
                } elseif (($status == 7 || $status == 8)
                    && ($val['arc_status'] == 0 || $val['arc_status'] == 1)
                    && ($val['return_check_status'] == 3 || $val['return_check_status'] == 4)
                ) { //待复检
                    $status_code = 83;
                } elseif ($status == 8 && ($val['cdc_status'] == 0 || $val['cdc_status'] == 1)
                    && ($val['check_car_status'] == 1 || $order['check_car_status'] == 2)
                ) {   //待验车
                    $status_code = 84;
                } elseif ($status == 8 && $val['check_car_status'] == 3 && $val['pay_status'] == 1) {   //已验车 -- 杭州
                    $status_code = 841;
                } elseif (!is_null($val['oi_status']) && $val['oi_status'] == 0) {    //车主待争议
                    $status_code = 86;
                } elseif ($status == 8 && $val['dealer_pay_status'] == 0) {   //待结算
                    $status_code = 849;
                } elseif ($status == 8 && ($val['dealer_pay_status'] == 1 || $val['dealer_pay_status'] == 2)) { //待收款
                    $status_code = 85;
                } elseif ($status == 8 && $val['delivery_mode'] == 2 && $val['dealer_pay_status'] == 3) {    //已收款 --北京
                    $status_code = 851;
                } elseif ($status == 12 && $val['fail_type'] == 9) {    //平台确认失败
                    $status_code = 119;
                } elseif ($status == 12 && $val['fail_type'] == 10) {   //自收完成导致失败的拍单
                    $status_code = 1210;
                } else {
                    $status_code = $status;
                }

                $pay_status = $val['pay_status'];
                $pay_status_code = 0;
                $pay_status_type = 0;
                if ($pay_status == -1) {
                    $pay_status_code = -1;
                } elseif ($pay_status == 1) {
                    $pay_status_code = 1;
                } elseif ($pay_status == 2) {
                    if ($val['car_source'] == 1) {    //4s店
                        $pay_status_code = 2;
                        $pay_status_type = 21;
                    } elseif ($val['car_source'] == 2) {  //个人
                        if ($val['three_in_one'] == 1) {
                            $pay_status_code = 2;
                            $pay_status_type = 22;
                        } else {                                  //三证不合一待付尾款显示为已付首款（0%）
                            $pay_status_code = 2;
                            $pay_status_type = 23;
                        }
                    }
                } elseif ($pay_status == 3) {
                    $pay_status_code = 3;
                } elseif ($pay_status == 4) {
                    if ($val['car_source'] == 1) {        //4s店
                        $pay_status_code = 2;
                        $pay_status_type = 21;
                    } elseif ($val['car_source'] == 2) { //个人
                        $pay_status_code = 4;
                        $pay_status_type = 21;
                    }
                }

                $list[$key]['order_status'] = C('ORDER_STATUS')[$status_code];
                $list[$key]['pay_status'] = C("ORDER_PAY_STATUS")[$pay_status_code];
                $list[$key]['pay_status_type'] = $pay_status_type;
                $list[$key]['location_area'] = $location_area;
            }
        }

        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }


    /**
     * 拍品查询 - 拍单列表
     *
     * @param integer $page
     * @param integer $size
     * @param array $params
     *
     * @return multitype:array(list,total)
     */
    public function get_export_order_list($params)
    {
        $map = array();
        /**
         * 条件查询
         * 1、竞拍时间 2、拍单状态 3、来源 4、拍单编号 5、级别 6、车牌号
         */
        if ($params['start_time']) {
            $map['o.bidding_start_time'] = array('egt', $params['start_time'] . ' 00:00:00');
        }
        if ($params['end_time']) {
            $map['o.bidding_start_time'] = array('elt', $params['end_time'] . ' 23:59:59');
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['o.bidding_start_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
        }
        if ($params['order_status']) {
            $order_status = $params['order_status'];
            if ($order_status == 122) {
                $map['o.status'] = array('eq', '12');
                $map['o.fail_type'] = array('eq', '2');
            } elseif ($order_status == 123) {
                $map['o.status'] = array('eq', '12');
                $map['o.fail_type'] = array('eq', '3');
            } elseif ($order_status == 126) {
                $map['o.status'] = array('eq', '12');
                $map['o.fail_type'] = array('eq', '6');
            } elseif ($order_status == 12) {
                $map['o.status'] = array('eq', '12');
                $map['o.fail_type'] = array('NOT IN', '2,3,6');
            } else {
                $map['o.status'] = array('eq', $order_status);
            }
        }
        if ($params['comefrom'] != '-2') {
            $map['co.comefrom'] = array('eq', $params['comefrom']);
        }
        if ($params['order_no']) {
            $order_no = trim($params['order_no']);
            $map['o.order_no'] = array('LIKE', '%' . $order_no . '%');
        }
        if ($params['car_level']) {
            $car_level = trim($params['car_level']);
            $car_level_front = substr($car_level, 0, -1);
            $car_level_last = substr($car_level, -1);
            if (ctype_digit($car_level)) {
                $map['c.composite_state'] = array('LIKE', '%' . $car_level . '%');
            }
            if (ctype_alpha($car_level)) {
                $map['c.accident_level'] = array('LIKE', '%' . $car_level . '%');
            }
            if ($car_level_front && ctype_alpha($car_level_last)) {
                $map['_string'] = "c.composite_state='" . $car_level_front . "' and c.accident_level='" . strtoupper($car_level_last) . "'";
            }
        }
        if ($params['car_plate_number']) {
            $car_plate_number = trim($params['car_plate_number']);
            $car_plate_number_first = mb_substr($car_plate_number, 0, 1, 'UTF8');
            $car_plate_number_last = mb_substr($car_plate_number, 1, 7, 'UTF8');
            $plate_prefix = array_search($car_plate_number_first, C('PLATE_PREFIX'));
            $plate_suffix = substr($car_plate_number, 1);
            $map['c.plate_prefix'] = array('eq', $plate_prefix);
            if ($car_plate_number_last) {
                $map['c.plate_suffix'] = array('LIKE', '%' . $car_plate_number_last . '%');
            }
        }
        $cols = "c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.first_reg_date,c.pointer_mileage
                ,c.reg_city,c.emission,c.composite_state
                ,c.accident_level,c.car_status,c.plate_prefix,c.plate_suffix,c.transfer_times
                ,co.owner_id,co.checker_id,co.comefrom,co.check_date
                ,o.order_id,o.order_no,o.status,o.fail_type,o.bid_start_price
                ,o.bid_best_price,o.bidding_best_price,o.reserve_price,o.show_reserve_price,o.bidding_start_time";
        $order = "o.order_id DESC";
        $total = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->where($map)->count();
        $list = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->field($cols)
            ->where($map)
            ->order($order)
            ->select();
//        echo M()->getLastSql();
        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                $location_area = $addr->get_name_by_code($val['location_area']);
                $list[$key]['order_no'] = $val['order_no'];
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                $list[$key]['car_level'] = $val['composite_state'] . $val['accident_level'];
                $list[$key]['car_plate_number'] = C('PLATE_PREFIX')[$val['plate_prefix']] . $val['plate_suffix'];
                if ($val['bid_best_price'] >= $val['bidding_best_price']) {
                    $best_price = format_money($val['bid_best_price']);
                } else {
                    $best_price = format_money($val['bidding_best_price']);
                }
                $list[$key]['best_price'] = $best_price; //;format_money($val['bidding_best_price']);
                $list[$key]['bid_start_price'] = format_money($val['bid_start_price']);
                $list[$key]['reserve_price'] = format_money($val['reserve_price']);
                $list[$key]['bidding_start_time'] = date("Y年m月d日", strtotime($val['bidding_start_time']));
                $fail_type = $val['fail_type'];
                if ($val['status'] == 12) {
                    if ($fail_type == 2) {
                        $status = 122;
                    } elseif ($fail_type == 3) {
                        $status = 123;
                    } elseif ($fail_type == 6) {
                        $status = 126;
                    } else {
                        $status = $val['status'];
                    }
                } else {
                    $status = $val['status'];
                }
                $list[$key]['order_status'] = $status;
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取场次列表
     *
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     *
     * @return multitype:array(list,total)
     */
    public function scene_list($page = 1, $size = 10, $paramer)
    {
        $map = array();
        //所属城市
        if (!empty($paramer['location_area'])) {
            $map['city'] = array('in', $paramer['location_area']);
        }
        //关键字(场次名称)
        if (!empty($paramer['keyword'])) {
            $map['name'] = array('like', '%' . $paramer['keyword'] . '%');
        }
        $total = M('order_scene')->where($map)->count();
        $list = M('order_scene')->where($map)->limit($size)->page($page)->order("scene_id DESC")->select();
        //echo M()->getLastSql();
        $a = new \AuctionCore\Other\Address();
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            $list[$key]['status_cn'] = C('SCENE_STATUS.' . $val['status']);
            $list[$key]['city'] = $a->get_name_by_code($val['city']);
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取场次拍品列表
     *
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     *
     * @return multitype:array(list,total)
     */
    public function scene_auction_list($page = 1, $size = 10, $paramer)
    {
        $map = array();
        //场次id
        if ($paramer['scene_id']) {
            $map['o.scene_id'] = array('eq', $paramer['scene_id']);
        }
        $map['o.rank'] = array('gt', '0');

        //拍单状态
        if ($paramer['order_status']) {
            $s = $paramer['order_status'] > 2 ? "gt" : "eq";
            $map['o.status'] = array($s, $paramer['order_status']);
        }
        //起拍时间
        if ($paramer['bidding_start_time']) {
            $map['o.bidding_start_time'] = array('like', $paramer['bidding_start_time'] . '%');
        }
        //关键字(拍卖单号，等级)
        if (!empty($paramer['keyword'])) {

            $where = array();
            $where['o.order_no'] = array('eq', urldecode($paramer['keyword']));
            $where['c.composite_state'] = array('eq', urldecode($paramer['keyword']));
            $where['c.accident_level'] = array('eq', urldecode($paramer['keyword']));
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }

        $cols = "o.order_id,o.order_no,o.car_id,o.status,o.coupon,o.transfer_fee,o.commision,o.bid_start_price,o.est_elapsed_time,o.bidding_start_time
                    ,o.is_timing_order,os.starttime,c.car_no,c.brand_id,c.series_id,c.model_id,c.reserve_price,c.composite_state,c.accident_level,o.rank";

        $total = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
            ->join(C('DB_PREFIX') . "order_scene as os ON o.`scene_id` = os.`scene_id`")
            ->where($map)->count();

        $list = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
            ->join(C('DB_PREFIX') . "order_scene as os ON o.`scene_id` = os.`scene_id`")
            ->where($map)->limit($size)->page($page)->order("o.is_timing_order DESC,o.`rank`,o.`order_id`")->select();
        //echo M('order')->getLastSql();
        $cm = new CarModel();
        //数据处理
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            $list[$key]['reserve_price'] = format_money($val['reserve_price']);
            $list[$key]['est_elapsed_time'] = floor($val['est_elapsed_time'] / 60);
            $list[$key]['status_cn'] = C('ORDER_STATUS.' . $val['status']);
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取待确认的车主列表
     *
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     *
     * @return array(list,total)
     */
    public function owner_confirm_list($page = 1, $size = 10, $paramer)
    {
        $map = array();
        $order = "o.order_id";
        //拍单状态
        $map['o.status'] = array('eq', '5'); //5 成交待确认
        $map['o.confirm_type'] = ['eq', 0];
        if ($paramer['status'] != '0') {
            if (!$paramer['auth']) {
                $map['o.owner_confirm_service_id'] = array('eq', $paramer['emp_id']); //分配给自己跟踪的拍单
            } else {
                $map['o.owner_confirm_service_id'] = array('gt', 0);
            }
        } else {
            $map['o.owner_confirm_service_id'] = array('eq', '0'); //尚未分配客服跟踪的拍单
        }
        if ($paramer['status'] == 1) {
            $map['o.is_consult'] = 0;
            $order = 'co.modify_time desc';
        }
        if ($paramer['status'] == '2') {
            $map['o.status'] = array('eq', '12');
            $map['o.is_consult'] = 0;
            $map['o.fail_type'] = array('eq', '1'); //待重拍,拍单状态为拍单失败,失败类型为车主要求重拍
        }
        if ($paramer['status'] == '3') {
            $map['o.status'] = array(array('egt', '7'), array('neq', '6'), array('neq', '12')); //7 待签约 排除拍单失败的拍单
            $map['o.is_consult'] = array('eq', 0);
            $map['ocl.result'] = ['eq', 1];
            $order = "o.last_time desc";
        }
        if ($paramer['status'] == '4') {
            $map['o.status'] = array(array('egt', '5')); //拍单状态 大于7且不等于12
            $map['o.is_consult'] = array('eq', 1);
            $order = "o.last_time desc";
        }
        if ($paramer['status'] == '5') {
            $map['o.status'] = array('eq', '12'); //12 拍单失败（失败类型(1车主要求重拍,2车主不卖了,3签约失败,4车商违约,5过户失败,6车主不卖(流))）
            //$map['o.fail_type'] = array(array('eq', '2'), array('eq', '6'), 'OR');
            $map['o.fail_type'] = array('in', array('2', '6'));
            $map['o.is_consult'] = array('eq', 0);
            $map['ocl.createtime'] = array('gt', 0);
            //$order = 'co.modify_time desc';
            $order = 'ocl.createtime desc';
        }
        //拍单号
        if (!empty($paramer['order_no'])) {
            $map['o.order_no'] = $paramer['order_no'];
        }
        //车主确定时间
        if (!empty($paramer['confirm_start_time']) || !empty($paramer['confirm_end_time'])) {
            if (empty($paramer['confirm_start_time'])) {
                $map['ocl.createtime'] = array("elt", $paramer['confirm_start_time'] . " 23:59:59");
            } elseif (empty($paramer['confirm_end_time'])) {
                $map['ocl.createtime'] = array("gt", $paramer['confirm_end_time'] . " 00:00:00");
            } else {
                $map['ocl.createtime'] = array(array("gt", $paramer['confirm_start_time'] . " 00:00:00"), array("elt", $paramer['confirm_end_time'] . " 23:59:59"));
            }
        }
        //跟踪时间
        if (!empty($paramer['trace_start_time']) || !empty($paramer['trace_end_time'])) {
            if (empty($paramer['trace_start_time'])) {
                $map['co.next_trace_time'] = array("elt", $paramer['trace_end_time'] . " 23:59:59");
            } elseif (empty($paramer['trace_end_time'])) {
                $map['co.next_trace_time'] = array("gt", $paramer['trace_start_time'] . " 00:00:00");
            } else {
                $map['co.next_trace_time'] = array(array("gt", $paramer['trace_start_time'] . " 00:00:00"), array("elt", $paramer['trace_end_time'] . " 23:59:59"));
            }
        }

        //车辆所在地
        if (!empty($paramer['location_area'])) {
            $map['c.location_area'] = array('in', $paramer['location_area']);
        }
        $cols = "o.order_id,o.order_no,o.car_id,o.status,o.bidding_best_price,o.bid_best_price,o.success_price,o.company_subsidies,o.is_consult
                    ,c.car_no,c.brand_id,c.series_id,c.model_id,c.reserve_price,c.transfer_limit,c.location_area,c.is_valid
                    ,co.owner_id,co.seller_name,co.phone,co.is_account_send,o.fail_type,c.car_status,c.car_source
                    ,co.sign_reserve_time,co.modify_time,co.next_trace_time,ocl.createtime as owner_confirm_time,o.owner_confirm_service_id";

        if ($paramer['status'] == '5') {
            $total = M('owner_confirm_log')->alias('ocl')
                ->join(C('DB_PREFIX') . "order as o ON ocl.order_id=o.order_id")
                ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
                ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
                ->where($map)->count();
            //echo M()->getLastSql();

            $list = M('owner_confirm_log')->alias('ocl')->field($cols)
                ->join(C('DB_PREFIX') . "order as o ON ocl.order_id=o.order_id")
                ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
                ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
                ->where($map)->limit($size)->page($page)->order($order)->select();
        } else {
            $total = M('order')->alias('o')
                ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
                ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
                ->join(' LEFT JOIN ' . C('DB_PREFIX') . "owner_confirm_log as ocl ON ocl.order_id=o.order_id")
                ->where($map)->count();


            $list = M('order')->alias('o')->field($cols)
                ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
                ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
                ->join(' LEFT JOIN ' . C('DB_PREFIX') . "owner_confirm_log as ocl ON ocl.order_id=o.order_id")
                ->where($map)->limit($size)->page($page)->order($order)->select();
        }
        //echo M()->getLastSql();
        $cm = new CarModel();
        $a = new \AuctionCore\Other\Address();
        //数据处理
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            //所在地
            $list[$key]['location_area'] = $a->get_name_by_code($val['location_area']);
            //车型
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            $list[$key]['best_price'] = format_money($val['bidding_best_price'] > $val['bid_best_price'] ? $val['bidding_best_price'] : $val['bid_best_price']);
            $list[$key]['reserve_price'] = format_money($val['reserve_price']);
            if ($paramer['status'] == 0) {
                $list[$key]['status_cn'] = "待确认";
            } elseif ($paramer['status'] == 1) {
                $list[$key]['status_cn'] = "跟踪中";
            } elseif ($paramer['status'] == 2) {
                $list[$key]['status_cn'] = "流拍";
            } elseif ($paramer['status'] == 3) {
                $list[$key]['status_cn'] = "确认成功";
            } elseif ($paramer['status'] == 4) {
                $list[$key]['status_cn'] = "磋商成功";
            } elseif ($paramer['status'] == 5) {
                $list[$key]['status_cn'] = "确认失败";
            }

            $list[$key]['phone'] = $val['phone'];
            $list[$key]['phone_has_star'] = showHideMobile($val['phone']);
            if ($paramer['auth']) {
                $owner_server = Employee::user_info_id($val['owner_confirm_service_id']);
                $list[$key]['owner_service'] = $owner_server['real_name'];
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取平台确认的拍单列表
     *
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     *
     * @return array(list,total)
     */
    public function platform_confirm_list($page = 1, $size = 10, $paramer)
    {
        $map = array();
        $order = "o.order_id";
        //拍单状态
        $map['o.confirm_type'] = ['eq', 1];
        $map['o.is_other_channel_deal'] = ['eq', 0];
        if ($paramer['status'] != '0') {
            if (!$paramer['auth']) {
                $map['o.platform_confirm_service_id'] = ['eq', $paramer['emp_id']]; //分配给自己跟踪的拍单
            } else {
                $map['o.platform_confirm_service_id'] = ['gt', 0];
            }
        } else {
            $map['o.status'] = ['eq', '5'];
            $map['o.platform_confirm_service_id'] = ['eq', '0']; //尚未分配客服跟踪的拍单
        }
        if ($paramer['status'] == 1) {
            $map['o.status'] = ['eq', '5'];
            $order = 'co.modify_time desc';
        }
        if ($paramer['status'] == '3') {
//            $map['o.status'] = [['egt', '7'], ['neq', '6'], ['neq', '12']]; //7 待签约 排除拍单失败的拍单
            $map['_string'] = '(o.status >= 7 AND o.status != 6 AND o.status != 12) OR 
                               (o.status = 12 AND (o.fail_type = 11 OR o.fail_type = 12))';
            $order = "o.last_time desc";
        }
        if ($paramer['status'] == '5') {
            $map['o.status'] = ['eq', '12']; //12 拍单失败（失败类型(1车主要求重拍,2车主不卖了,3签约失败,4车商违约,5过户失败,6车主不卖(流))）
            $map['o.fail_type'] = ['eq', '9'];
            $order = 'co.modify_time desc';
        }
        //拍单号
        if (!empty($paramer['order_no'])) {
            $map['o.order_no'] = $paramer['order_no'];
        }
        //平台确定时间
        if (!empty($paramer['confirm_start_time']) || !empty($paramer['confirm_end_time'])) {
            if (empty($paramer['confirm_start_time'])) {
                $map['pcl.createtime'] = array("elt", $paramer['confirm_start_time'] . " 23:59:59");
            } elseif (empty($paramer['confirm_end_time'])) {
                $map['pcl.createtime'] = array("gt", $paramer['confirm_end_time'] . " 00:00:00");
            } else {
                $map['pcl.createtime'] = array(array("gt", $paramer['confirm_start_time'] . " 00:00:00"), array("elt", $paramer['confirm_end_time'] . " 23:59:59"));
            }
        }
        //跟踪时间
        if (!empty($paramer['trace_start_time']) || !empty($paramer['trace_end_time'])) {
            if (empty($paramer['trace_start_time'])) {
                $map['co.next_trace_time'] = array("elt", $paramer['trace_end_time'] . " 23:59:59");
            } elseif (empty($paramer['trace_end_time'])) {
                $map['co.next_trace_time'] = array("gt", $paramer['trace_start_time'] . " 00:00:00");
            } else {
                $map['co.next_trace_time'] = array(array("gt", $paramer['trace_start_time'] . " 00:00:00"), array("elt", $paramer['trace_end_time'] . " 23:59:59"));
            }
        }
        //车辆所在地
        if (!empty($paramer['location_area'])) {
            $map['c.location_area'] = array('in', $paramer['location_area']);
        }
        $cols = "o.order_id,o.order_no,o.car_id,o.status,o.bidding_best_price,o.bid_best_price,o.success_price,
                 o.company_subsidies,o.platform_confirm_service_id,o.confirm_type,o.success_dealer_id,
                 c.car_no,c.brand_id,c.series_id,c.model_id,c.reserve_price,c.transfer_limit,c.location_area,c.is_valid,
                 c.self_receive_dealer_id,c.car_status,c.is_dealer_breach,c.is_self_receive,
                 co.owner_id,o.fail_type,co.comefrom,co.comefrom_url,co.next_trace_time,pcl.createtime as platform_confirm_time";
        $prefix = C('DB_PREFIX');
        $total = M('order')->alias('o')
            ->join($prefix . "cars as c ON c.car_id = o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id = c.owner_id")
            ->join(' LEFT JOIN ' . C('DB_PREFIX') . "platform_confirm_log as pcl ON pcl.order_id = o.order_id AND pcl.result = 1")
            ->where($map)->count();

        $list = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id = o.car_id")
            ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id = c.owner_id")
            ->join(' LEFT JOIN ' . C('DB_PREFIX') . "platform_confirm_log as pcl ON pcl.order_id = o.order_id AND pcl.result = 1")
            ->where($map)->limit($size)->page($page)->order($order)->select();
        //echo M()->getLastSql();
        $cm = new CarModel();
        $a = new \AuctionCore\Other\Address();
        //数据处理
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            //所在地
            $list[$key]['location_area'] = $a->get_name_by_code($val['location_area']);
            //车型
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            $list[$key]['best_price'] = format_money($val['bidding_best_price'] > $val['bid_best_price'] ? $val['bidding_best_price'] : $val['bid_best_price']);
            $list[$key]['reserve_price'] = format_money($val['reserve_price']);
            if ($paramer['status'] == 0) {
                $list[$key]['status_cn'] = "待确认";
            } elseif ($paramer['status'] == 1) {
                $list[$key]['status_cn'] = "跟踪中";
            } elseif ($paramer['status'] == 3) {
                $list[$key]['status_cn'] = "确认成功";
            } elseif ($paramer['status'] == 5) {
                $list[$key]['status_cn'] = "确认失败";
            }
            $dealer = Dealer::getDealerById($val['success_dealer_id']);
            if (!empty($dealer)) {
                $d = new Dealer();
                $dealer = $d->get_contact_mobile_byAntNestfind($dealer);
            }
            $list[$key]['dealer_contact_person'] = $dealer['contact_person'] ? $dealer['contact_person'] : '--';
            $list[$key]['dealer_contact_mobile'] = $dealer['contact_mobile'] ? $dealer['contact_mobile'] : '--';
            $list[$key]['dealer_phone_has_star'] = $dealer['contact_mobile'] ? showHideMobile($dealer['contact_mobile']) : '--';
            if ($val['is_dealer_breach'] == 1) {
                $list[$key]['platform_confirm_type_name'] = '违约重拍';
            }
            if ($val['is_self_receive'] == 1) {
                $list[$key]['platform_confirm_type_name'] = '自收重拍';
            }
            if ($paramer['auth']) {
                $platform_confirm_server = Employee::user_info_id($val['platform_confirm_service_id']);
                $list[$key]['platform_service'] = $platform_confirm_server['real_name'];
            }
            $list[$key]['next_trace_time'] = $val['next_trace_time'] > 0 ? $val['next_trace_time'] : '--';
            //获取确认失败时间
            if ($paramer['status'] == 5) {
                $AuOrder = new AuOrder($val['order_id']);
                if ($val['is_dealer_breach'] == 1) {
                    $platform_log = $AuOrder->get_platform_confirm_log(['result' => 4]);
                    $list[$key]['platform_conform_failed_time'] = $platform_log['createtime'];
                }
                if ($val['is_self_receive'] == 1) {
                    $platform_log = $AuOrder->get_platform_confirm_log(['result' => 3]);
                    $list[$key]['platform_conform_failed_time'] = $platform_log['createtime'];
                }

            }

        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }


    /**
     * 获取待确认的车商联系人列表
     *
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     *
     * @return multitype:array(list,total)
     */
    public function dealer_confirm_list($page = 1, $size = 10, $paramer)
    {
        $map = array();

        //拍单状态
        $map['o.status'] = array('eq', '8'); //8 侍付首款（已和车主签约）
        $map['da.status'] = 0; //车商拍到的车(0,正常 1,无效 2,第一名违约，第二名未确认前的中间状态)
        $map['o.dealer_confirm_status'] = array('lt', '4'); //车商确认状态(1,待确认 2,第一名车商失败 3,第二名车商失败 4,待付款 5,付款成功
        if ($paramer['status'] != '0') {//跟踪中
            if (!$paramer['auth']) {
                $map['o.dealer_confirm_service_id'] = array('eq', $paramer['emp_id']); //分配给自己跟踪的拍单
            } else {
                $map['o.dealer_confirm_service_id'] = array('gt', 0);
            }
        } else {//待确认
            $map['o.dealer_confirm_service_id'] = array('eq', '0'); //尚未分配客服跟踪的拍单
        }
        //待车商付款
        if ($paramer['status'] == '2') {
            $map['o.dealer_confirm_status'] = array('eq', '4');
        }
        //车商付款成功
        if ($paramer['status'] == '3') {
            unset($map['o.status']);
            $map['o.dealer_confirm_status'] = array('eq', '5'); // 5付款成功
        }
        //拍单失败
        if ($paramer['status'] == '4') {
            $map['o.dealer_confirm_status'] = array(array('eq', '2'), array('eq', '3'), 'OR');
            $map['o.status'] = array('eq', '12'); //12 拍单失败
            $map['da.status'] = 1;
        }
        if (!empty($paramer['order_no'])) {
            $map['o.order_no'] = array('LIKE', '%' . $paramer['order_no'] . '%');
        }

        //车辆所在地
        if (!empty($paramer['location_area'])) {
            $map['c.location_area'] = array('in', $paramer['location_area']);
        }

        $cols = "o.order_id,o.order_no,o.car_id,o.status,o.bidding_best_price,o.bid_best_price,o.dealer_confirm_status
                    ,o.dealer_confirm_service_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.reserve_price,c.location_area
                    ,co.sign_date,cd.contact_person,cd.contact_mobile";

        $total = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
            ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
            ->join(C('DB_PREFIX') . "dealer_auctioned as da ON da.order_id=o.order_id")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.dealer_id=da.dealer_id")
            ->where($map)->count();

        $list = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
            ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
            ->join(C('DB_PREFIX') . "dealer_auctioned as da ON da.order_id=o.order_id")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.dealer_id=da.dealer_id")
            ->where($map)->limit($size)->page($page)->order("o.order_id")->select();
        $cm = new CarModel();
        $a = new \AuctionCore\Other\Address();
        //数据处理
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            //所在地
            $list[$key]['location_area'] = $a->get_name_by_code($val['location_area']);
            //车型
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            $list[$key]['best_price'] = format_money($val['bidding_best_price'] > $val['bid_best_price'] ? $val['bidding_best_price'] : $val['bid_best_price']);
            $list[$key]['status_cn'] = C('DEALER_CONFIRM_STATUS.' . $val['dealer_confirm_status']);
            $list[$key]['contact_mobile_has_star'] = showHideMobile($val['contact_mobile']);
            if ($paramer['auth']) {
                $dealer_server = Employee::user_info_id($val['dealer_confirm_service_id']);
                $list[$key]['dealer_service'] = $dealer_server['real_name'];
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取流拍的车辆列表
     *
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     *
     * @return multitype:array(list,total)
     */
    public function unsold_list($page = 1, $size = 10, $paramer)
    {
        $car = new CarModel;
        $map = array();
        $where = array();
        /**
         * 搜索条件
         * 1、品牌
         * 2、车系
         * 3、车型
         * 4、拍单id
         */
        if ($paramer['brand_id']) {
            //根据主品牌id获取子品牌
            $brand_info = $car->get_brand_by_mbrand($paramer['brand_id']);
            if (!empty($brand_info)) {
                foreach ($brand_info as $v1) {
                    $brand_list[] = $v1['brand_id'];
                }
                if (!empty($brand_list)) {
                    $map['c.brand_id'] = array("in", $brand_list);
                }
            }
        } else {
            //获取所有主品牌
            $mbrands_info = $car->get_mbrands();
            foreach ($mbrands_info as $v) {
                $mbrands_list[] = $v['mbrand_id'];
            }
            if (!empty($mbrands_list)) {
                $map['c.mbrand_id'] = array("in", $mbrands_list);
            }
        }

        //车系
        if ($paramer['series_id']) {
            $map['c.series_id'] = array("eq", $paramer['series_id']);
        } else {
            if ($paramer['brand_id']) {
                //根据主品牌id获取子品牌
                $brand_info = $car->get_brand_by_mbrand($paramer['brand_id']);
                if (!empty($brand_info)) {
                    foreach ($brand_info as $v1) {
                        $brand_list[] = $v1['brand_id'];
                    }
                }
                $series_info = $car->get_series_by_mbrand($paramer['brand_id']);
                if (!empty($series_info)) {
                    foreach ($series_info as $v2) {
                        $series_list[] = $v2['series_id'];
                    }
                    if (!empty($series_list)) {
                        $map['c.series_id'] = array('in', $series_list);
                    }
                }
            }
        }

        //车型
        if ($paramer['model_id']) {
            $map['c.model_id'] = array("eq", $paramer['model_id']);
        } else {
            if ($paramer['brand_id'] && $paramer['series_id']) {
                $model_info = $car->get_model_by_series($paramer['series_id']);
                if (!empty($model_info)) {
                    foreach ($model_info as $vm) {
                        $model_list[] = $vm['model_id'];
                    }
                    if (!empty($model_list)) {
                        $map['c.model_id'] = array("in", $model_list);
                    }
                }

//                $brand_info = $car->get_brand_by_mbrand($paramer['brand_id']);
//                if (!empty($brand_info)) {
//                    foreach ($brand_info as $v1) {
//                        $brand_list[] = $v1['brand_id'];
//                    }
//                }
//                $series_info = $car->get_series_by_mbrand($paramer['brand_id']);
//                if (!empty($series_info)) {
//                    foreach ($series_info as $v2) {
//                        $series_list[] = $v2['series_id'];
//                    }
//                    if (!empty($series_list)) {
//                        foreach ($series_list as $v3) {
//                            $cars_model[] = $car->get_model_by_series($v3);
//                        }
//                        $cars_model = array_filter($cars_model);
//                        foreach ($cars_model as $v4) {
//                            foreach ($v4 as $v5) {
//                                $model_list[] = $v5['model_id'];
//                            }
//                        }
//                        if(!empty($model_list)){
//                            $map['c.model_id']  = array('in',$model_list);
//                        }
//                    }
//                } 
            }
        }
        if ($paramer['paidan_id']) {
            $map['o.order_no'] = array("eq", $paramer['paidan_id']);
        }

        if ($paramer['status'] == '0') {
            $map['o.status'] = array('eq', '6'); //6 拍单流拍
            $map['o.unsold_service_id'] = array('eq', '0'); //尚未分配客服跟踪的拍单
        }
        if ($paramer['status'] == '1') {
            $where['o.status'] = array('eq', '6'); //6 拍单流拍
            $where['c.car_status'] = array('eq', '5'); //车源状态为5 拍卖中
        }
        if ($paramer['status'] == '2') {
            $where['o.status'] = array('eq', '12');
            $where['o.fail_type'] = array('eq', '1'); //待重拍,拍单状态为拍单失败,失败类型为车主要求重拍
            $where['c.car_status'] = array('eq', '7'); //车源状态为7 待重拍
        }
        if ($paramer['status'] == '3') {//磋商成功 拍卖流程继续向下走，
            $where['o.status'] = array(array('egt', '7'), array('neq', '12')); //拍单状态 大于7且不等于12
            $where['is_consult'] = array('eq', 1);
        }
        if ($paramer['status'] == '4') {//磋商失败 拍卖失败状态 拍单失败，原因车主不卖了
            $where['o.status'] = array('eq', '12'); //12 拍单失败（失败类型(1车主要求重拍,2车主不卖了,3签约失败,4车商违约,5过户失败)）
            $where['o.fail_type'] = array('eq', 6);
            //$where['is_consult'] = array('eq',1);
        }
        if ($paramer['status'] > '0') {
            if (!$paramer['auth']) {
                $where['o.unsold_service_id'] = array('eq', $paramer['emp_id']); //自己跟踪的拍单
            } else {
                $where['o.unsold_service_id'] = array('gt', 0); //自己跟踪的拍单
            }
            $map['_complex'] = $where;
        }

        $cols = "o.order_id,o.order_no,o.car_id,o.status,o.fail_type,o.bid_best_price,o.bidding_best_price,o.unsold_service_id
                    ,c.car_no,c.car_status,c.brand_id,c.series_id,c.model_id,o.reserve_price,c.reserve_price_history
                    ,co.owner_id,co.seller_name,co.phone";

        $total = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
            ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->count();

        $list = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
            ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->limit($size)->page($page)->order("o.order_id desc ")->select();
        $cm = new CarModel();
        // echo M()->getLastSql();
        //数据处理
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            $list[$key]['best_price'] = format_money($val['bidding_best_price'] > $val['bid_best_price'] ? $val['bidding_best_price'] : $val['bid_best_price']);
            $list[$key]['status_cn'] = C('ORDER_STATUS.' . $val['status']);
            $list[$key]['phone'] = $val['phone'];
            $list[$key]['phone_has_star'] = showHideMobile($val['phone']);
            $list[$key]['reserve_price_history'] = json_decode($val['reserve_price_history'], true);
            if ($paramer['auth']) {
                $unsold_server = Employee::user_info_id($val['unsold_service_id']);
                $list[$key]['unsold_servicer'] = $unsold_server['real_name'];
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 车源展示，获取流拍车辆（磋商失败、拍单失败）
     *
     * @param type $page
     * @param type $size
     * @param type $paramer
     *
     * @return type
     */
    public function car_unsold_list($page = 1, $size = 10, $paramer)
    {
        $car = new CarModel;
        $map = array();
        $where = array();
        /**
         * 搜索条件
         * 1、品牌
         * 2、车系
         * 3、车型
         * 4、状态：待上架、销售中、销售成功
         * 5、标签：车主急售、高性价比
         * 6、车辆编号|级别
         */
        if ($paramer['brand_id']) {
            //根据主品牌id获取子品牌
            $brand_info = $car->get_brand_by_mbrand($paramer['brand_id']);
            if (!empty($brand_info)) {
                foreach ($brand_info as $v1) {
                    $brand_list[] = $v1['brand_id'];
                }
                if (!empty($brand_list)) {
                    $map['c.brand_id'] = array("in", $brand_list);
                }
            }
        } else {
            //获取所有主品牌
            $mbrands_info = $car->get_mbrands();
            foreach ($mbrands_info as $v) {
                $mbrands_list[] = $v['mbrand_id'];
            }
            if (!empty($mbrands_list)) {
                $map['c.mbrand_id'] = array("in", $mbrands_list);
            }
        }

        //车系
        if ($paramer['series_id']) {
            $map['c.series_id'] = array("eq", $paramer['series_id']);
        } else {
            if ($paramer['brand_id']) {
                //根据主品牌id获取子品牌
                $brand_info = $car->get_brand_by_mbrand($paramer['brand_id']);
                if (!empty($brand_info)) {
                    foreach ($brand_info as $v1) {
                        $brand_list[] = $v1['brand_id'];
                    }
                }
                $series_info = $car->get_series_by_mbrand($paramer['brand_id']);
                if (!empty($series_info)) {
                    foreach ($series_info as $v2) {
                        $series_list[] = $v2['series_id'];
                    }
                    if (!empty($series_list)) {
                        $map['c.series_id'] = array('in', $series_list);
                    }
                }
            }
        }

        //车型
        if ($paramer['model_id']) {
            $map['c.model_id'] = array("eq", $paramer['model_id']);
        } else {
            if ($paramer['brand_id'] && $paramer['series_id']) {
                $model_info = $car->get_model_by_series($paramer['series_id']);
                if (!empty($model_info)) {
                    foreach ($model_info as $vm) {
                        $model_list[] = $vm['model_id'];
                    }
                    if (!empty($model_list)) {
                        $map['c.model_id'] = array("in", $model_list);
                    }
                }
            }
        }
//        $where['o.status'] = array('eq', '6');              //6 拍单流拍
//        $where['o.unsold_service_id'] = array('eq', '0');   //尚未分配客服跟踪的拍单
//        $where['_string'] = "o.status=6 AND o.unsold_service_id='0'";
//        $map['_complex'] = $where;
        $map['_logic'] = 'AND';
//        $map['o.status'] = array('eq', '12');               //拍单失败
//        $map['o.unsold_service_id'] = array('eq', $paramer['emp_id']); //自己跟踪的拍单
//        $map['_string'] = "(o.status=6 AND o.unsold_service_id='0') OR (o.status=12 AND o.unsold_service_id=" . $paramer['emp_id'] . ")";
//        $map['o.status'] = array('eq', '12');
//        $where_str = '';
        //销售状态
        if ($paramer['car_status']) {
            $map['cs.car_status'] = array('eq', $paramer['car_status']);
        }
        //销售标签
        if ($paramer['car_mark']) {
            $map['cs.car_marks'] = array('like', '%' . $paramer['car_mark'] . '%');
        }
        //车辆编号
        if ($paramer['car_no_state']) {
            $map['c.car_no'] = array('eq', $paramer['car_no_state']);
        }
        $map['o.fail_type'] = array("eq", 6);
        $map['o.unsold_service_id'] = array('gt', 0);
        $cols = "DISTINCT(o.car_id),c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.reserve_price,c.reserve_price_history
                ,c.composite_state,c.accident_level
                ,co.owner_id,co.seller_name,co.phone
                ,cs.sale_id,cs.car_status,cs.car_marks,cs.car_success_price";

        $total = M('cars')->alias('c')->field($cols)
            ->join(C('DB_PREFIX') . "order as o ON c.car_id = o.car_id")
            ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
            ->join(" LEFT JOIN " . C('DB_PREFIX') . "car_sale as cs ON cs.car_id=c.car_id")
            ->where($map)
            ->count("DISTINCT(o.car_id)");
        $list = M('cars')->alias('c')->field($cols)
            ->join(C('DB_PREFIX') . "order as o ON c.car_id = o.car_id")
            ->join(C('DB_PREFIX') . "car_owner as co ON co.owner_id=c.owner_id")
            ->join(" LEFT JOIN " . C('DB_PREFIX') . "car_sale as cs ON cs.car_id=c.car_id")
            ->where($map)
            ->limit($size)
            ->page($page)
            ->order('o.order_id DESC')
            ->select();
//        echo M()->getLastSql();
//        header('xxxxxxxxxxxxxxx:' . M()->getLastSql());
        $cm = new CarModel();
        //数据处理
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
            $list[$key]['composite_level'] = $val['composite_state'] . $val['accident_level'];
            $list[$key]['reserve_price'] = format_money($val['reserve_price']);
            $list[$key]['best_price'] = format_money($val['bidding_best_price'] > $val['bid_best_price'] ? $val['bidding_best_price'] : $val['bid_best_price']);
            $list[$key]['car_success_price'] = format_money($val['car_success_price']);
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取签约列表
     *
     * @param integer $page
     * @param int $size
     * @param unknown $paramer
     *
     * @return multitype:unknown string
     */
    public function sign_list($page = 1, $size = 10, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;
        $order = "o.last_time desc";
        $map = [];
        //拍单状态
        $map['o.status'] = array('eq', '7'); //7 确认待签约
        $map['o.return_check_status'] = [['eq', 5], ['eq', 7], 'or']; //复检成功或协商成功
        //1待分配，2待签约，3跟踪中，4签约成功，5签约失败 6.调度 签约成功 7.调度 签约失败
        if ($params['status'] != 6 || $params['status'] != 7) {
            if (!$params['auth']) {
                $map['co.deliver_id'] = array("eq", $params['emp_id']);
            }
        }
        if ($params['status'] == 1) {//待分配
            $map['co.deliver_id'] = array("eq", 0);
            $order = "co.sign_reserve_time desc";
        }

        if ($params['status'] == 2) {
            $map['co.deliver_id'] = array("gt", 0);
            $map['o.is_sign_tracing'] = array("eq", 0); //待签约
            $order = "co.sign_reserve_time desc";
        }
        if ($params['status'] == 3) {
            $map['o.is_sign_tracing'] = array("eq", 1); //跟踪中
            $order = "co.sign_next_trace_time desc";
        }
        if ($params['status'] == 4) {
            $map['o.status'] = array('eq', 8); //签约成功
            $order = "co.sign_date desc";
        }
        if ($params['status'] == 5) {
            $map['o.status'] = array('eq', 12);
            $map['o.fail_type'] = array('eq', 3); //签约失败
        }
        if ($params['status'] == 6) {
            $map['o.status'] = array('eq', 8); //调度页面 签约成功 不做限制默认可查看所有
            $order = "co.sign_date desc";
        }
        if ($params['status'] == 7) {
            $map['o.status'] = array('eq', 12);
            $map['o.fail_type'] = array('eq', 3); //调度页面 签约失败 不做限制默认可查看所有
        }

        //拍单号
        if (!empty($params['order_no'])) {
            $map['o.order_no'] = $params['order_no'];
        }
        if (!empty($params['bc_city'])) {
            $map['c.location_area'] = array('in', $params['bc_city']);
        }
        //签约预约时间
        if (!empty($params['sign_start_time']) || !empty($params['sign_end_time'])) {
            if (empty($params['sign_start_time'])) {
                $map['co.sign_reserve_time'] = array("elt", $params['sign_end_time' . ' 23:00:00']);
            } elseif (empty($params['sign_end_time'])) {
                $map['co.sign_reserve_time'] = array("gt", $params['sign_start_time' . ' 00:00:00']);
            } else {
                $map['co.sign_reserve_time'] = array(array("gt", $params['sign_start_time'] . ' 00:00:00'), array("elt", $params['sign_end_time'] . ' 23:00:00'));
            }
        }
        //跟踪时间
        if (!empty($params['trace_start_time']) || !empty($params['trace_end_time'])) {
            if (empty($params['trace_start_time'])) {
                $map['co.next_trace_time'] = array("elt", $params['trace_end_time'] . " 23:59:59");
            } elseif (empty($params['trace_end_time'])) {
                $map['co.next_trace_time'] = array("gt", $params['trace_start_time'] . " 00:00:00");
            } else {
                $map['co.next_trace_time'] = array(array("gt", $params['trace_start_time'] . " 00:00:00"), array("elt", $params['trace_end_time'] . " 23:59:59"));
            }
        }
        //车主电话
        if (!empty($params['owner_phone'])) {
            $map['co.phone'] = $params['owner_phone'];
        }
        //车主姓名
        if (!empty($params['seller_name'])) {
            $map['co.seller_name'] = $params['seller_name'];
        }
        //签约预约省、市、区
        if (!empty($params['province'])) {
            $map['co.sign_reserve_province'] = $params['province'];
        }
        if (!empty($params['city'])) {
            $map['co.sign_reserve_city'] = $params['city'];
        }
        if (!empty($params['province'])) {
            $map['co.sign_reserve_area'] = $params['area'];
        }

        //要查询的列表
        $cols = "o.order_id,o.order_no,o.status,o.car_id,o.last_time,o.is_lock,c.car_source,
            c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.location_area,co.seller_name,co.phone,co.sign_reserve_time,
            co.sign_reserve_province,co.sign_reserve_store,co.sign_service_method,co.sign_date,
            co.sign_reserve_city,co.sign_reserve_area,co.sign_reserve_address,co.deliver_id,o.is_sign_tracing";
        $prefix = C('DB_PREFIX');
        $total = M('order')->alias('o')
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->count();

        $list = M('order')->alias('o')->field($cols)
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->limit($size)->page($page)->order($order)->select();
        //echo M()->getLastSql();
        $cm = new CarModel();
        $addr = new \AuctionCore\Other\Address();
        $store = new \AuctionCore\Other\Store();
        //数据处理
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                $list[$key] = $val;
                if ($list[$key]['is_sign_tracing'] == 0) {
                    $list[$key]['sign_reserve_time'] = transform_date($val['sign_reserve_time'], 1);
                }
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                if ($val['sign_service_method'] == 2) {
                    $province = $addr->get_name_by_code($val['sign_reserve_province']);
                    $city = $addr->get_name_by_code($val['sign_reserve_city']);
                    $area = $addr->get_name_by_code($val['sign_reserve_area']);
                    $list[$key]['address'] = $province . " " . $city . " " . $area . " " . $val['sign_reserve_address'];
                } elseif ($val['sign_service_method'] == 3) {
                    $address = $store->detail(array('store_id' => $val['sign_reserve_store']));
                    $list[$key]['address'] = $address['store_name'];
                } else {
                    $list[$key]['address'] = "未知";
                }
                $list[$key]['bc_city_name'] = $addr->get_name_by_code($val['location_area']);
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                if ($params['auth']) {
                    $deliver = Employee::user_info_id($val['deliver_id']);
                    $list[$key]['deliver_name'] = $deliver['real_name'];
                }
                //是否显示违约
                $list[$key]['is_again_void'] = true;
                // $changeInfo = changeOrder::detailMap(['order_id'=>$val['order_id'],'order_status'=>$val['status']]);
                // if ($changeInfo) {
                //     $list[$key]['is_again_void'] = false;
                // }
                if ($val['is_lock'] == 1) {
                    $list[$key]['is_again_void'] = false;
                }
                if ($val['car_source'] == 2) {
                    $list[$key]['is_again_void'] = false;
                }
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 过户列表
     *
     * @param int $page
     * @param int $size
     * @param array $params
     *
     * @return multitype:array $result
     */
    public function transfer_list($page = 1, $size = 10, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;
        if (!empty($params)) {
            $map = $this->make_transfer_params($params);
        }

        $prefix = C('DB_PREFIX');
        //要查询的列
        $cols = "o.order_id,o.order_no,o.status, o.car_id,c.car_id,c.plate_prefix,c.plate_suffix,c.car_no,c.brand_id,c.series_id,c.model_id,c.location_area,
                co.seller_name,co.deliver_id,co.phone,co.comefrom,co.comefrom_url,da.dealer_id,
                cd.contact_person,cd.contact_mobile";
        $order = "o.last_time DESC";
        if ($params['status'] == 2) {
            $cols .= ',l.createtime as transfer_start_time';
        } elseif ($params['status'] == 3) {
            $cols .= ',l.createtime as transfer_success_time';
        } elseif (($params['status'] == 4)) {
            $cols .= ',l.createtime as transfer_fail_time';
        }

        $total = M('order')->alias('o')->field($cols)
            ->join($prefix . "dealer_auctioned as da ON da.order_id = o.order_id and da.status=0")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id = da.dealer_id")
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->join("LEFT JOIN " . $prefix . "transfer_log as l ON l.order_id=o.order_id")
            ->where($map)->count();

        $list = M('order')->alias('o')->field($cols)
            ->join($prefix . "dealer_auctioned as da ON da.order_id = o.order_id and da.status=0")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id = da.dealer_id")
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->join("LEFT JOIN " . $prefix . "transfer_log as l ON l.order_id=o.order_id")
            ->where($map)->limit($size)->page($page)->order($order)->select();
        // echo M()->getlastsql();
        $cm = new CarModel();
        $addr = new \AuctionCore\Other\Address();
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            if ($params['auth']) {
                $deliver = Employee::user_info_id($val['deliver_id']);
                $list[$key]['deliver_name'] = $deliver['real_name'];
            }
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);
            $issue_params = [
                'order_id' => $val['order_id']
            ];
            if ($val['status'] == 9) {
                $issue_params['issue_type'] = 3;
            } elseif ($val['status'] == 10) {
                $issue_params['issue_type'] = 5;
            }
            $list[$key]['is_issue'] = M("dealer_issue")->where($issue_params)->find();
            $list[$key]['plate_number'] = C("PLATE_PREFIX." . $val['plate_prefix']) . $val['plate_suffix'];
            $list[$key]['bc_city_name'] = $addr->get_name_by_code($val['location_area']);
            // $dealer_info = \AuctionCore\CarDealer\Dealer::getDealerById($val['dealer_id']);
            // $list[$key]['contact_person'] = $dealer_info['contact_person'];
            // $list[$key]['contact_mobile'] = $dealer_info['contact_mobile'];
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 过户列表
     *
     * @param array $params
     *
     * @return multitype:multitype:string number  multitype:string unknown  NULL multitype:multitype:string unknown
     */
    private function make_transfer_params($params)
    {
        $map = array();
        //拍单状态
        if (!$params['auth']) {
            $map['co.deliver_id'] = $params['emp_id'];
        }
        if ($params['status'] == 1) {
            $map['l.result'] = array(array('eq', 0), array('EXP', 'IS NULL'), 'or');
            $map['o.status'] = array('eq', 9);
        }
        if ($params['status'] == 2) {
            $map['o.status'] = array('eq', 10); //过户中
            $map['l.result'] = array('eq', 1);
        }
        if ($params['status'] == 3) {
            //$map['o.status'] = array('eq', 11); //过户完成，待付尾款
            $map['l.result'] = array('eq', 2);
        }
        if ($params['status'] == 4) {
            $map['l.result'] = array('eq', 3);
        }
        //过户时间
        if (!empty($params['start_time']) || !empty($params['end_time'])) {
            if (empty($params['start_time'])) {
                $map['l.createtime'] = array("elt", $params['end_time'] . " 23:59:59");
            } elseif (empty($params['end_time'])) {
                $map['l.createtime'] = array("egt", $params['start_time'] . " 00:00:00");
            } else {
                $map['l.createtime'] = array(array("egt", $params['start_time'] . " 00:00:00"), array("elt", $params['end_time'] . " 23:59:59 "));
            }
        }
        if (!empty($params['bc_city'])) {
            $map['c.location_area'] = array("in", $params['bc_city']);
        }

        //拍单号
        if (!empty($params['order_no'])) {
            $map['o.order_no'] = $params['order_no'];
        }

        //车商联系人
        if (!empty($params['contact_person'])) {
            $map['cd.contact_person'] = $params['contact_person'];
        }
        //车商电话
        if (!empty($params['contact_mobile'])) {
            $map['cd.contact_mobile'] = $params['contact_mobile'];
        }
        //车牌号
        if ($params['car_plate_number']) {
            $car_plate_number = trim($params['car_plate_number']);
            $car_plate_number_first = mb_substr($car_plate_number, 0, 1, 'UTF8');
            $car_plate_number_last = mb_substr($car_plate_number, 1, 7, 'UTF8');
            $plate_prefix = array_search($car_plate_number_first, C('PLATE_PREFIX'));
            $plate_suffix = substr($car_plate_number, 1);
            $map['c.plate_prefix'] = array('eq', $plate_prefix);
            if ($car_plate_number_last) {
                $map['c.plate_suffix'] = array('LIKE', '%' . $car_plate_number_last . '%');
            }
        }
        return $map;
    }

    /**
     * 过户日志列表
     *
     * @param int $page
     * @param int $size
     * @param array $params
     *
     * @return multitype:array $result
     */
    public function transfer_log_list($page = 1, $size = 10, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;
        if (!empty($params)) {
            $map = $this->make_transfer_params($params);
        }

        $prefix = C('DB_PREFIX');
        //要查询的列
        $cols = "o.order_id,o.order_no,o.status, o.car_id,c.car_id,c.car_no,c.brand_id,c.series_id,c.model_id,c.location_area,
                co.seller_name,co.deliver_id,co.phone,co.comefrom,co.comefrom_url,da.dealer_id,
                cd.contact_person,cd.contact_mobile";
        $order = "o.last_time DESC";
        if ($params['status'] == 2) {
            $transferStatus = 1;
            $cols .= ',l.createtime as transfer_start_time';
        } else {
            $transferStatus = 2;
            $cols .= ',l.createtime as transfer_success_time';
        }
        $total = M('order')->alias('o')->field($cols)
            ->join($prefix . "dealer_auctioned as da ON da.order_id = o.order_id and da.status=0")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id = da.dealer_id")
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->join("LEFT JOIN " . $prefix . "transfer_log as l ON l.order_id=o.order_id and l.result=" . $transferStatus)
            ->where($map)->count();

        $list = M('order')->alias('o')->field($cols)
            ->join($prefix . "dealer_auctioned as da ON da.order_id = o.order_id and da.status=0")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id = da.dealer_id")
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->join("LEFT JOIN " . $prefix . "transfer_log as l ON l.order_id=o.order_id and l.result=" . $transferStatus)
            ->where($map)->limit($size)->page($page)->order($order)->select();
        // echo M()->getLastSql();
        $cm = new CarModel();
        $addr = new \AuctionCore\Other\Address();
        foreach ($list as $key => $val) {
            $list[$key] = $val;
            if ($params['auth']) {
                $deliver = Employee::user_info_id($val['deliver_id']);
                $list[$key]['deliver_name'] = $deliver['real_name'];
            }
            $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $model = $cm->get_model_by_id($val['model_id']);

            $list[$key]['bc_city_name'] = $addr->get_name_by_code($val['location_area']);
            // $dealer_info = \AuctionCore\CarDealer\Dealer::getDealerById($val['dealer_id']);
            // $list[$key]['contact_person'] = $dealer_info['contact_person'];
            // $list[$key]['contact_mobile'] = $dealer_info['contact_mobile'];
            $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 今日拍品统计,车主，第一名车商
     */
    public function stat_list($page = 1, $size = 10, $paramer)
    {
        $paramer['starttime'] = I("get.starttime");
        $paramer['endtime'] = I("get.endtime");
        $paramer['order_no'] = I("get.order_no");
        $paramer['car_no'] = I("get.car_no");
        $paramer['mbrand_id'] = I("get.mbrand_id");
        $paramer['series_id'] = I("get.series_id");
        $paramer['level'] = I("get.level");
        $paramer['contact_person'] = I("get.contact_person");

        $map = array();
        $cond = '';
        $cond1 = '';
        //$cond .= " AND os.status ='3'";
        $cond .= " AND o.`status` >4 and o.`status` <100";

        //
        if ($paramer['starttime']) {
            $cond .= " AND os.starttime >= '" . $paramer['starttime'] . " 00:00:00" . "'";
            $cond1 .= " AND `bidding_end_time` >= '" . $paramer['starttime'] . " 00:00:00" . "'";
        }

        if ($paramer['endtime']) {
            $cond .= " AND os.starttime <= '" . $paramer['endtime'] . " 23:59:59" . "'";
            $cond1 .= " AND `bidding_end_time` <= '" . $paramer['endtime'] . " 23:59:59" . "'";
        }

        if ($paramer['order_no']) {
            $cond .= " AND o.order_no='" . $paramer['order_no'] . "'";
        }
        if ($paramer['car_no']) {
            $cond .= " AND car.car_no='" . $paramer['car_no'] . "'";
        }
        if ($paramer['mbrand_id']) {
            $cond .= " AND car.mbrand_id='" . $paramer['mbrand_id'] . "'";
        }
        if ($paramer['series_id']) {
            $cond .= " AND car.series_id='" . $paramer['series_id'] . "'";
        }
        if ($paramer['level']) {
            preg_match('/(\d+)([a-zA-Z]+)/', $paramer['level'], $arr);
            $cond .= " AND car.composite_state = '" . $arr[1] . "'";
            $cond .= " AND car.accident_level = '" . $arr[2] . "'";
        }
        if ($paramer['contact_person']) {
            $cond .= " AND d.contact_person like '%" . $paramer['contact_person'] . "%'";
        }

        //sql
        $sql = "
select o.order_id,o.order_no,o.status,o.bidding_start_time,o.bidding_end_time,car.car_id,car.car_no,car.`series_id` ,cs.`name` series_name,
car.`composite_state` ,car.`accident_level` ,car.emission,car.car_status,e.real_name,e.username,ow.comefrom,car.transfer_limit,
(case car.`gearbox` WHEN 1 THEN '手动'
    WHEN 2 THEN '自动'
      WHEN 3 THEN '手自一体'
      WHEN 4 THEN 'CVT'
      WHEN 5 THEN '双离合'
end) `gearbox`,
car.`car_cc` ,
(CASE car.`turbo` WHEN 1 THEN 'T'
 WHEN 0 THEN 'L' END
) `turbo`,o.`bid_start_price`,o.reserve_price,o.show_reserve_price,c.price,c.createtime bidtime,car.suggest_max_price,car.suggest_min_price,car.first_reg_date,car.car_cc,car.car_color,c.bidtype,ow.seller_name,ow.phone,ow.checker_id,d.dealer_id,d.fullname,d.contact_person,d.contact_mobile
,os.`scene_id` ,os.`name` scene_name
from
 `au_order` o left join
(
    select * from (
        select * from
        (
            /*找出所有出价记录*/
            SELECT `order_id`,`dealer_id`,`price`,`createtime` ,'bid' bidtype FROM au_bid_log a INNER JOIN (SELECT MAX(bid_id) bid_id FROM au_bid_log WHERE order_id in(
            (select `order_id` from `au_order` where 1=1 " . $cond1 . " and `status` >4 and `status` <100)
            ) GROUP BY dealer_id) b ON a.`bid_id` = b.bid_id
            union
            SELECT  `order_id`,`dealer_id`,`after_price` `price`,`createtime`,'bidding' bidtype from `au_bidding_log` WHERE `order_id` in
            (select `order_id` from `au_order` where 1=1 " . $cond1 . " and `status` >4 and `status` <100)
         ) a order by order_id,price desc,createtime desc
     ) b group by order_id,dealer_id order by order_id,price desc
) c  on c.order_id=o.`order_id`
left join `au_cars` car on o.car_id=car.car_id
left join `au_car_series` cs on car.`series_id` = cs.`series_id`
left join `au_car_owner` ow on car.owner_id = ow.owner_id
left join `au_car_dealer` d on c.dealer_id = d.dealer_id
left join `au_order_scene` os on o.`scene_id` = os.`scene_id`
left join `au_employee` e on e.emp_id=ow.checker_id
where 1=1 " . $cond . " order by price desc";


        $list = M()->query($sql);
        $arr = array();
        foreach ($list as $val) {
            //$arr[$val['order_id']]=$val['order_id']%2==0?'E4F3E6':'308bff';
            $arr[$val['order_id']] = $val['order_id'];
        }
        $arr1 = array();
        $i = 0;
        foreach ($arr as $order_id) {
            $arr1[$order_id] = ($i % 2 == 0) ? 'E4F3E6' : '308bff';
            $i++;
        }
        //数据处理
        foreach ($list as $key => $val) {
            $list[$key]['style'] = $arr1[$val['order_id']];
            $list[$key]['emission'] = C('EMISSION.' . $val['emission']);
            $list[$key]['car_color'] = C('CAR_COLOR.' . $val['car_color']);
            $list[$key]['status'] = C('ORDER_STATUS.' . $val['status']);
            $list[$key]['car_status'] = C('CAR_STATUS.' . $val['car_status']);
            $list[$key]['transfer_limit'] = C('TRANSFER_LIMIT.' . $val['transfer_limit']);
        }
        $result = array(
            'list' => $list,
            'total' => count($list)
        );
        return $result;
    }

    /*
     * 今日拍品统计
     * */

    public function stat_owner_list($page = 1, $size = 10, $paramer)
    {
        $paramer['starttime'] = I("get.starttime");
        $paramer['endtime'] = I("get.endtime");
        $map = array();
        if ($paramer['starttime']) {
            $map['starttime'] = "createtime >='" . $paramer['starttime'] . " 00:00:00'";
        } else {
            $map['starttime'] = "createtime >='" . date('Y-m-d') . " 00:00:00'";
        }
        if ($paramer['endtime']) {
            $map['endtime'] = "createtime <='" . $paramer['endtime'] . " 23:59:59'";
        } else {
            $map['endtime'] = "createtime <='" . date('Y-m-d') . " 23:59:59'";
        }
        $cond = implode(" AND ", $map);
        //场次id
        $sql = "SELECT a.`order_no`,d.`name`,
         (case b.`gearbox` WHEN 1 THEN '手动'
        WHEN 2 THEN '自动'
          WHEN 3 THEN '手自一体'
          WHEN 4 THEN 'CVT'
          WHEN 5 THEN '双离合'
        end) `gearbox` ,
        b.`composite_state` ,
        b.`accident_level` ,
        b.`car_cc` ,
        (CASE b.`turbo` WHEN 1 THEN 'T'
         WHEN 0 THEN 'L' END
        ) `turbo` ,b.`reserve_price` ,a.`scene_id` ,os.name as scene_name,
        a.`bid_start_price`,b.`suggest_min_price` ,b.`suggest_max_price` ,c.`seller_name` ,c.`phone`   FROM `au_order` a
        INNER JOIN `au_cars` b on a.`car_id` =b.`car_id` and a.`status`>4 and a.`status`<100 and
        a.`order_id` in(SELECT `order_id` FROM `au_bid_log`  WHERE (" . $cond . ") union SELECT `order_id` from
                                                    `au_bidding_log` WHERE (" . $cond . "))
        INNER JOIN `au_car_owner` c on b.`owner_id` =c.`owner_id`
        INNER JOIN `au_car_series` d on b.`series_id` =d.`series_id`
        INNER JOIN `au_order_scene` os on a.`scene_id` = os.scene_id
        ";

        $list = M()->query($sql);
        //数据处理
        foreach ($list as $key => $val) {

        }
        $result = array(
            'list' => $list,
            'total' => count($list)
        );
        return $result;
    }

    /*
     * 今日拍品最高价统计
     * #统计日内出价排序信息，并找出车辆，车主，车商相关信息
     * */

    public function stat_dealer_list($page = 1, $size = 10, $paramer)
    {
        $paramer['starttime'] = I("get.starttime");
        $paramer['endtime'] = I("get.endtime");
        $map = array();
        if ($paramer['starttime']) {
            $map['starttime'] = "createtime >='" . $paramer['starttime'] . " 00:00:00'";
        } else {
            $map['starttime'] = "createtime >='" . date('Y-m-d') . " 00:00:00'";
        }
        if ($paramer['endtime']) {
            $map['endtime'] = "createtime <='" . $paramer['endtime'] . " 23:59:59'";
        } else {
            $map['endtime'] = "createtime <='" . date('Y-m-d') . " 23:59:59'";
        }
        $cond = implode(" AND ", $map);

        $sql = "
        select o.order_id,o.order_no,car.car_id,car.car_no, (case car.`gearbox` WHEN 1 THEN '手动'
        		WHEN 2 THEN '自动'
          		WHEN 3 THEN '手自一体'
          		WHEN 4 THEN 'CVT'
          		WHEN 5 THEN '双离合'
        end) `gearbox`,
        car.`car_cc` ,
        (CASE car.`turbo` WHEN 1 THEN 'T'
         WHEN 0 THEN 'L' END
        ) `turbo`,c.price,c.bidtype,ow.seller_name,ow.phone,d.dealer_id,d.contact_person,d.contact_mobile
        from (
            select * from (
                select * from
                (
                    SELECT `order_id`,`dealer_id`,`price`,`createtime` ,'bid' bidtype  FROM `au_bid_log` WHERE (" . $cond . ")
                    union
                    SELECT  `order_id`,`dealer_id`,`after_price` `price`,`createtime`,'bidding' bidtype  from `au_bidding_log` WHERE (" . $cond . ")
                 )  a  order by order_id,price desc,createtime desc
             ) b   group by order_id,dealer_id order by order_id,price desc
        ) c
        left join `au_order` o on c.order_id=o.`order_id`
        left join `au_cars` car on o.car_id=car.car_id
        left join `au_car_owner` ow on car.owner_id = ow.owner_id
        left join `au_car_dealer` d on c.dealer_id = d.dealer_id
        ";
        $list = M()->query($sql);
        //数据处理
        foreach ($list as $key => $val) {

        }
        $result = array(
            'list' => $list,
            'total' => count($list)
        );
        return $result;
    }

    /**
     * 历史成交记录列表
     *
     * @param type $page
     * @param type $size
     * @param type $params
     *
     * @return type
     */
    public function get_history_deal_list($page, $size, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;
        $map = array();
        $car = new CarModel;
        /**
         * 搜索条件：1、拍单编号 2、车型 3、状态 4、开始时间 - 结束时间
         */
        if ($params['order_no']) {
            $map['o.order_no'] = array("LIKE", '%' . $params['order_no'] . '%');
        }
        if ($params['brand_id']) {
            //根据主品牌id获取子品牌
            $brand_info = $car->get_brand_by_mbrand($params['brand_id']);
            if (!empty($brand_info)) {
                foreach ($brand_info as $v1) {
                    $brand_list[] = $v1['brand_id'];
                }
                if (!empty($brand_list)) {
                    $map['c.brand_id'] = array("in", $brand_list);
                }
            }
        } else {
            //获取所有主品牌
            $mbrands_info = $car->get_mbrands();
            foreach ($mbrands_info as $v) {
                $mbrands_list[] = $v['mbrand_id'];
            }
            if (!empty($mbrands_list)) {
                $map['c.mbrand_id'] = array("in", $mbrands_list);
            }
        }

        //车系
        if ($params['series_id']) {
            $map['c.series_id'] = array("eq", $params['series_id']);
        } else {
            if ($params['brand_id']) {
                //根据主品牌id获取子品牌
                $brand_info = $car->get_brand_by_mbrand($params['brand_id']);
                if (!empty($brand_info)) {
                    foreach ($brand_info as $v1) {
                        $brand_list[] = $v1['brand_id'];
                    }
                }
                $series_info = $car->get_series_by_mbrand($params['brand_id']);
                if (!empty($series_info)) {
                    foreach ($series_info as $v2) {
                        $series_list[] = $v2['series_id'];
                    }
                    if (!empty($series_list)) {
                        $map['c.series_id'] = array('in', $series_list);
                    }
                }
            }
        }

        //车型
        if ($params['model_id']) {
            $map['c.model_id'] = array("eq", $params['model_id']);
        } else {
            if ($params['brand_id'] && $params['series_id']) {
                $model_info = $car->get_model_by_series($params['series_id']);
                if (!empty($model_info)) {
                    foreach ($model_info as $vm) {
                        $model_list[] = $vm['model_id'];
                    }
                    if (!empty($model_list)) {
                        $map['c.model_id'] = array("in", $model_list);
                    }
                }
            }
        }

        if ($params['is_recommend']) {
            $map['o.is_recommend'] = array("eq", $params['is_recommend']);
        }
        if ($params['start_time']) {
            $map['o.trade_time'] = array('egt', $params['start_time'] . ' 00:00:00');
        }
        if ($params['end_time']) {
            $map['o.trade_time'] = array('elt', $params['end_time'] . ' 23:59:59');
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['o.trade_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
        }
        $map['status'] = '13';
        $order = 'o.order_id DESC';
        $cols = 'c.car_id,c.brand_id,c.series_id,c.model_id,c.first_reg_date,c.pointer_mileage'
            . ',o.order_id,o.order_no,o.bidding_best_price,o.success_price,o.trade_time,o.is_recommend';
        $total = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
            ->where($map)->limit($size)->count();
        $list = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON c.car_id=o.car_id")
            ->where($map)
            ->limit($size)
            ->page($page)
            ->order($order)
            ->select();
        $cm = new \AuctionCore\CarResource\CarModel();
        $list_data = array();
        foreach ($list as $kl => $vl) {
            $list_data[$kl]['car_id'] = $vl['car_id'];
            $list_data[$kl]['order_id'] = $vl['order_id'];
            $list_data[$kl]['order_no'] = $vl['order_no'];
            $brand = $cm->get_model_by_id($vl['brand_id'], 'brand');
            $series = $cm->get_model_by_id($vl['series_id'], 'series');
            $model = $cm->get_model_by_id($vl['model_id']);
            $list_data[$kl]['car_model'] = $brand['name'] . $series['name'] . $model['name'];
            $list_data[$kl]['success_price'] = format_money($vl['success_price']);
            $list_data[$kl]['bidding_best_price'] = format_money($vl['bidding_best_price']);
            $list_data[$kl]['trade_time'] = $vl['trade_time'];
            $list_data[$kl]['is_recommend'] = $vl['is_recommend'];
        }
        $result = array(
            'list' => $list_data,
            'total' => $total
        );
        return $result;
    }

    /**
     * 改变推荐、取消推荐的状态
     */
    public function history_deal_post($order_id, $is_recommend)
    {
        $condition['order_id'] = $order_id;
        $data['is_recommend'] = $is_recommend;
        return M('order')->where($condition)
            ->setField($data);
    }

    public function get_dealerbid_list($page = 1, $size = 10, $params)
    {
        $map = array();
        $car = new CarModel;
        /**
         * 条件查询
         * 1、品牌 2、车系 3、车型 4、排放标准  6、车辆来源  7、开始时间 结束时间
         */
        if ($params['brand_id']) {
            $map['c.brand_id'] = array("eq", $params['brand_id']);
            //根据主品牌id获取子品牌
            $brand_info = $car->get_brand_by_mbrand($params['brand_id']);
            if (!empty($brand_info)) {
                foreach ($brand_info as $v1) {
                    $brand_list[] = $v1['brand_id'];
                }
                if (!empty($brand_list)) {
                    $map['c.brand_id'] = array("in", $brand_list);
                }
            }
        }
        //车系
        if ($params['series_id']) {
            $map['c.series_id'] = array("eq", $params['series_id']);
        }
        //车型
        if ($params['model_id']) {
            $map['c.model_id'] = array("eq", $params['model_id']);
        }
        if (!empty($params['start_time']) || !empty($params['end_time'])) {
            if (empty($params['start_time'])) {
                $map['o.bidding_end_time'] = array(array("gt", "0000-00-00 00:00:00"), array("elt", $params['end_time'] . " 23:59:59"));
            } elseif (empty($params['end_time'])) {
                $map['o.bidding_end_time'] = array("egt", $params['start_time'] . " 00:00:00");
            } else {
                $map['o.bidding_end_time'] = array(array("egt", $params['start_time'] . " 00:00:00"), array("elt", $params['end_time'] . " 23:59:59"));
            }
        }

        if ($params['emission']) {
            $map['c.emission'] = array('eq', $params['emission']);
        }
        if ($params['comefrom'] != '-2') {
            $map['co.comefrom'] = array('eq', $params['comefrom']);
        }
        if ($params['order_no']) {
            $order_no = trim($params['order_no']);
            $map['o.order_no'] = array('LIKE', '%' . $order_no . '%');
        }
        if ($params['dealer_id']) {
            $map['bl.dealer_id'] = array('eq', $params['dealer_id']);
        }
        if ($params['order_mode']) {
            $map['o.status'] = array('eq', $params['order_mode']);
        }
        $cols = "bl.dealer_id,c.brand_id,c.series_id,c.model_id,c.composite_state,c.accident_level,c.emission,c.car_color,c.location_area,co.comefrom,o.order_id,o.bidding_end_time,o.status,o.order_no,cd.bail_amount,cd.freeze_amount,cd.indemnity_amount,GROUP_CONCAT(bl.createtime ORDER BY bl.createtime DESC) as createtime";
        $order = "o.order_id DESC";
        $total = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join('LEFT JOIN ' . C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->join(C('DB_PREFIX') . "bid_log as bl ON bl.`order_id`=o.`order_id`")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.`dealer_id`=bl.`dealer_id`")
            ->where($map)->group('bl.order_id,bl.dealer_id')->select();
        $total = count($total);
        $list = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->join(C('DB_PREFIX') . "bid_log as bl ON bl.`order_id`=o.`order_id`")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.`dealer_id`=bl.`dealer_id`")
            ->field($cols)
            ->where($map)
            ->group('bl.order_id,bl.dealer_id')
            ->order($order)
            ->limit($size)
            ->page($page)
            ->select();
        // echo M()->getlastsql();
        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            $emission = C('EMISSION');
            $car_color = C('CAR_COLOR');
            $order_status = C('NEW_ORDER_STATUS');
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                $location_area = $addr->get_name_by_code($val['location_area']);
                $list[$key]['order_no'] = $val['order_no'];
                $list[$key]['brand'] = $brand['name'];
                $list[$key]['series'] = $series['name'];
                $list[$key]['model'] = $model['name'];
                $list[$key]['car_level'] = $val['composite_state'] . $val['accident_level'];
                $list[$key]['emission'] = $emission[$val['emission']];
                $list[$key]['car_color'] = $car_color[$val['car_color']];
                $createtime = explode(',', $val['createtime'])[0];
                $xmap = array();
                $xmap['createtime'] = array('eq', $createtime);
                $price = M('bid_log')->where($xmap)->find();
                $list[$key]['price'] = $price['price'];
                $nmap = array();
                $nmap['dealer_id'] = array('eq', $val['dealer_id']);
                $nmap['order_id'] = array('eq', $val['order_id']);
                $status = M('dealer_auctioned')->field('status')->where($nmap)->find();
                if ((!$status || ($status && $status['status'] != 0)) && $val['status'] == 13) {
                    $list[$key]['order_status'] = $order_status[$val['status']] . "(你未竞得)";
                } else {
                    $list[$key]['order_status'] = $order_status[$val['status']];
                }
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    public function get_dealerbidding_list($page = 1, $size = 10, $params)
    {
        $map = array();
        $car = new CarModel;
        /**
         * 条件查询
         * 1、品牌 2、车系 3、车型 4、排放标准  6、车辆来源  7、开始时间 结束时间
         */
        if ($params['brand_id']) {
            $map['c.brand_id'] = array("eq", $params['brand_id']);
            //根据主品牌id获取子品牌
            $brand_info = $car->get_brand_by_mbrand($params['brand_id']);
            if (!empty($brand_info)) {
                foreach ($brand_info as $v1) {
                    $brand_list[] = $v1['brand_id'];
                }
                if (!empty($brand_list)) {
                    $map['c.brand_id'] = array("in", $brand_list);
                }
            }
        }
        //车系
        if ($params['series_id']) {
            $map['c.series_id'] = array("eq", $params['series_id']);
        }
        //车型
        if ($params['model_id']) {
            $map['c.model_id'] = array("eq", $params['model_id']);
        }
        if ($params['start_time']) {
            $map['bl.createtime'] = array('egt', $params['start_time'] . ' 00:00:00');
        }
        if ($params['end_time']) {
            $map['bl.createtime'] = array('elt', $params['end_time'] . ' 23:59:59');
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['bl.createtime'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
        }

        if ($params['bid_start_time']) {
            $map['o.bidding_end_time'] = array('egt', $params['bid_start_time'] . ' 00:00:00');
        }
        if ($params['bid_end_time']) {
            $map['o.bidding_end_time'] = array('elt', $params['bid_end_time'] . ' 23:59:59');
        }
        if ($params['bid_start_time'] && $params['bid_end_time']) {
            $map['o.bidding_end_time'] = array(array('egt', $params['bid_start_time'] . ' 00:00:00'), array('elt', $params['bid_end_time'] . ' 23:59:59'));
        }
        if ($params['emission']) {
            $map['c.emission'] = array('eq', $params['emission']);
        }
        if ($params['comefrom'] != '-2') {
            $map['co.comefrom'] = array('eq', $params['comefrom']);
        }
        if ($params['order_no']) {
            $order_no = trim($params['order_no']);
            $map['o.order_no'] = array('LIKE', '%' . $order_no . '%');
        }
        if ($params['dealer_id']) {
            $map['bl.dealer_id'] = array('eq', $params['dealer_id']);
        }
        if ($params['order_mode']) {
            $map['o.status'] = array('eq', $params['order_mode']);
        }
        $cols = "bl.dealer_id,c.brand_id,c.series_id,c.model_id,c.composite_state,c.accident_level,c.emission,c.car_color,c.location_area,co.comefrom,o.order_id,o.bidding_end_time,o.status,o.order_no,cd.bail_amount,cd.freeze_amount,cd.indemnity_amount,GROUP_CONCAT(bl.after_price ORDER BY bl.after_price DESC) as price";
        $order = "o.order_id DESC";
        $sql = M('order')->alias('o')->field("1")
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join('LEFT JOIN ' . C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->join(C('DB_PREFIX') . "bidding_log as bl ON bl.`order_id`=o.`order_id`")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.`dealer_id`=bl.`dealer_id`")
            ->where($map)
            ->group('bl.order_id,bl.dealer_id')
            ->buildSql();
        $sql = 'select count(1) as tp_sum from' . $sql . 'as tt';
        $result = M()->query($sql);
        $total = $result[0]['tp_sum'];
        $list = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->join(C('DB_PREFIX') . "bidding_log as bl ON bl.`order_id`=o.`order_id`")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.`dealer_id`=bl.`dealer_id`")
            ->where($map)
            ->group('bl.order_id,bl.dealer_id')
            ->order($order)
            ->limit($size)
            ->page($page)
            ->select();
        // echo M()->getlastsql();
        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            $emission = C('EMISSION');
            $car_color = C('CAR_COLOR');
            $order_status = C('NEW_ORDER_STATUS');
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                $location_area = $addr->get_name_by_code($val['location_area']);
                $list[$key]['order_no'] = $val['order_no'];
                $list[$key]['brand'] = $brand['name'];
                $list[$key]['series'] = $series['name'];
                $list[$key]['model'] = $model['name'];
                $list[$key]['car_level'] = $val['composite_state'] . $val['accident_level'];
                $list[$key]['emission'] = $emission[$val['emission']];
                $list[$key]['car_color'] = $car_color[$val['car_color']];
                $list[$key]['price'] = explode(',', $val['price'])[0];
                $nmap = array();
                $nmap['dealer_id'] = array('eq', $val['dealer_id']);
                $nmap['order_id'] = array('eq', $val['order_id']);
                $status = M('dealer_auctioned')->field('status')->where($nmap)->find();
                if ((!$status || ($status && $status['status'] != 0)) && $val['status'] == 13) {
                    $list[$key]['order_status'] = $order_status[$val['status']] . "(你未竞得)";
                } else {
                    $list[$key]['order_status'] = $order_status[$val['status']];
                }
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    public function get_dealerdeal_list($page = 1, $size = 10, $params)
    {
        $map = array();
        $car = new CarModel;
        /**
         * 条件查询
         * 1、品牌 2、车系 3、车型 4、排放标准  6、车辆来源  7、开始时间 结束时间
         */
        if ($params['brand_id']) {
            $map['c.brand_id'] = array("eq", $params['brand_id']);
            //根据主品牌id获取子品牌
            $brand_info = $car->get_brand_by_mbrand($params['brand_id']);
            if (!empty($brand_info)) {
                foreach ($brand_info as $v1) {
                    $brand_list[] = $v1['brand_id'];
                }
                if (!empty($brand_list)) {
                    $map['c.brand_id'] = array("in", $brand_list);
                }
            }
        }
        //车系
        if ($params['series_id']) {
            $map['c.series_id'] = array("eq", $params['series_id']);
        }
        //车型
        if ($params['model_id']) {
            $map['c.model_id'] = array("eq", $params['model_id']);
        }
        if ($params['start_time']) {
            $map['da.createtime'] = array('egt', $params['start_time'] . ' 00:00:00');
        }
        if ($params['end_time']) {
            $map['da.createtime'] = array('elt', $params['end_time'] . ' 23:59:59');
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['da.createtime'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
        }

        if ($params['bid_start_time']) {
            $map['o.bidding_end_time'] = array('egt', $params['bid_start_time'] . ' 00:00:00');
        }
        if ($params['bid_end_time']) {
            $map['o.bidding_end_time'] = array('elt', $params['bid_end_time'] . ' 23:59:59');
        }
        if ($params['bid_start_time'] && $params['bid_end_time']) {
            $map['o.bidding_end_time'] = array(array('egt', $params['bid_start_time'] . ' 00:00:00'), array('elt', $params['bid_end_time'] . ' 23:59:59'));
        }

        if ($params['emission']) {
            $map['c.emission'] = array('eq', $params['emission']);
        }
        if ($params['comefrom'] != '-2') {
            $map['co.comefrom'] = array('eq', $params['comefrom']);
        }
        if ($params['order_no']) {
            $order_no = trim($params['order_no']);
            $map['o.order_no'] = array('LIKE', '%' . $order_no . '%');
        }
        if ($params['dealer_id']) {
            $map['da.dealer_id'] = array('eq', $params['dealer_id']);
        }
        $map['o.status'] = array('eq', 13);
        $map["da.status"] = array('eq', 0);
        $cols = "da.dealer_id,c.brand_id,c.series_id,c.model_id,c.composite_state,c.accident_level,c.emission,c.car_color,c.location_area,co.comefrom,o.order_id,o.bidding_end_time,o.status,o.order_no,cd.bail_amount,cd.freeze_amount,cd.indemnity_amount,o.reserve_price,GROUP_CONCAT(da.trade_price ORDER BY da.trade_price DESC) as price";
        $order = "o.order_id DESC";
        $total = M('order')->alias('o')
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join('LEFT JOIN ' . C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->join(C('DB_PREFIX') . "dealer_auctioned as da ON da.`order_id`=o.`order_id`")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.`dealer_id`=da.`dealer_id`")
            ->where($map)->group('da.order_id,da.dealer_id')->select();
        $total = count($total);
        $list = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->join(C('DB_PREFIX') . "dealer_auctioned as da ON da.`order_id`=o.`order_id`")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.`dealer_id`=da.`dealer_id`")
            ->where($map)
            ->group('da.order_id,da.dealer_id')
            ->order($order)
            ->limit($size)
            ->page($page)
            ->select();
        // echo M()->getlastsql();
        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            $emission = C('EMISSION');
            $car_color = C('CAR_COLOR');
            $order_status = C('NEW_ORDER_STATUS');
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                $location_area = $addr->get_name_by_code($val['location_area']);
                $list[$key]['order_no'] = $val['order_no'];
                $list[$key]['brand'] = $brand['name'];
                $list[$key]['series'] = $series['name'];
                $list[$key]['model'] = $model['name'];
                $list[$key]['car_level'] = $val['composite_state'] . $val['accident_level'];
                $list[$key]['emission'] = $emission[$val['emission']];
                $list[$key]['car_color'] = $car_color[$val['car_color']];
                $list[$key]['price'] = explode(',', $val['price'])[0];
                $list[$key]['order_status'] = $order_status[$val['status']];
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    public function get_dealerfollow_list($page = 1, $size = 10, $params)
    {
        $map = array();
        $car = new CarModel;
        /**
         * 条件查询
         * 1、品牌 2、车系 3、车型 4、排放标准  6、车辆来源  7、开始时间 结束时间
         */
        if ($params['brand_id']) {
            $map['c.brand_id'] = array("eq", $params['brand_id']);
            //根据主品牌id获取子品牌
            $brand_info = $car->get_brand_by_mbrand($params['brand_id']);
            if (!empty($brand_info)) {
                foreach ($brand_info as $v1) {
                    $brand_list[] = $v1['brand_id'];
                }
                if (!empty($brand_list)) {
                    $map['c.brand_id'] = array("in", $brand_list);
                }
            }
        }
        //车系
        if ($params['series_id']) {
            $map['c.series_id'] = array("eq", $params['series_id']);
        }
        //车型
        if ($params['model_id']) {
            $map['c.model_id'] = array("eq", $params['model_id']);
        }
        // if ($params['start_time']) {
        //     $map['o.bid_start_time'] = array('egt', $params['start_time'] . ' 00:00:00');
        // }
        // if ($params['end_time']) {
        //     $map['o.bid_start_time'] = array('elt', $params['end_time'] . ' 23:59:59');
        // }
        // if ($params['start_time'] && $params['end_time']) {
        //     $map['o.bid_start_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
        // }
        if (!empty($params['start_time']) || !empty($params['end_time'])) {
            if (empty($params['start_time'])) {
                $map['o.bid_start_time'] = array(array("gt", "0000-00-00 00:00:00"), array("elt", $params['end_time'] . " 23:59:59"));
            } elseif (empty($params['end_time'])) {
                $map['o.bid_start_time'] = array("egt", $params['start_time'] . " 00:00:00");
            } else {
                $map['o.bid_start_time'] = array(array("egt", $params['start_time'] . " 00:00:00"), array("elt", $params['end_time'] . " 23:59:59"));
            }
        }
        if ($params['bid_start_time']) {
            $map['o.bidding_end_time'] = array('egt', $params['bid_start_time'] . ' 00:00:00');
        }
        if ($params['bid_end_time']) {
            $map['o.bidding_end_time'] = array('elt', $params['bid_end_time'] . ' 23:59:59');
        }
        if ($params['bid_start_time'] && $params['bid_end_time']) {
            $map['o.bidding_end_time'] = array(array('egt', $params['bid_start_time'] . ' 00:00:00'), array('elt', $params['bid_end_time'] . ' 23:59:59'));
        }

        if ($params['emission']) {
            $map['c.emission'] = array('eq', $params['emission']);
        }
        if ($params['comefrom'] != '-2') {
            $map['co.comefrom'] = array('eq', $params['comefrom']);
        }
        if ($params['order_no']) {
            $order_no = trim($params['order_no']);
            $map['o.order_no'] = array('LIKE', '%' . $order_no . '%');
        }
        if ($params['dealer_id']) {
            $map['df.dealer_id'] = array('eq', $params['dealer_id']);
        }
        if ($params['order_mode']) {
            $map['o.status'] = array('eq', $params['order_mode']);
        }
        $cols = "c.brand_id,c.series_id,c.model_id,c.composite_state,c.accident_level,c.emission,c.car_color,c.location_area,co.comefrom,o.order_id,o.bidding_end_time,o.bidding_best_price,o.bid_best_price,o.status,o.order_no,cd.bail_amount,cd.freeze_amount,cd.indemnity_amount,df.dealer_id";
        $order = "o.order_id DESC";
        $total = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join('LEFT JOIN ' . C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->join(C('DB_PREFIX') . "dealer_follow as df ON df.`order_id`=o.`order_id`")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.`dealer_id`=df.`dealer_id`")
            ->where($map)->group('df.order_id,df.dealer_id')->select();
        $total = count($total);
        $list = M('order')->alias('o')->field($cols)
            ->join(C('DB_PREFIX') . "cars as c ON o.`car_id`=c.`car_id`")
            ->join(C('DB_PREFIX') . "car_owner as co ON c.`owner_id`=co.`owner_id`")
            ->join(C('DB_PREFIX') . "dealer_follow as df ON df.`order_id`=o.`order_id`")
            ->join(C('DB_PREFIX') . "car_dealer as cd ON cd.`dealer_id`=df.`dealer_id`")
            ->where($map)
            ->group('df.order_id,df.dealer_id')
            ->order($order)
            ->limit($size)
            ->page($page)
            ->select();
        // echo M()->getlastsql();
        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            $emission = C('EMISSION');
            $car_color = C('CAR_COLOR');
            $order_status = C('NEW_ORDER_STATUS');
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                $location_area = $addr->get_name_by_code($val['location_area']);
                $list[$key]['order_no'] = $val['order_no'];
                $list[$key]['brand'] = $brand['name'];
                $list[$key]['series'] = $series['name'];
                $list[$key]['model'] = $model['name'];
                $list[$key]['car_level'] = $val['composite_state'] . $val['accident_level'];
                $list[$key]['emission'] = $emission[$val['emission']];
                $list[$key]['car_color'] = $car_color[$val['car_color']];
                $nmap = array();
                $nmap['dealer_id'] = array('eq', $val['dealer_id']);
                $nmap['order_id'] = array('eq', $val['order_id']);

                $dealer_bid = M('bid_log')->field('GROUP_CONCAT(price ORDER BY price DESC) as price')->where($nmap)->group('dealer_id,order_id')->find();
                $dealer_bidding = M('bidding_log')->field('GROUP_CONCAT(after_price ORDER BY after_price DESC) as price')->where($nmap)->group('dealer_id,order_id')->find();
                $dealer_auctioned = M('dealer_auctioned')->where($nmap)->find();
                $bid_price = explode(',', $dealer_bid['price'])[0];
                $bidding_price = explode(',', $dealer_bidding['price'])[0];
                if (($bid_price || $bidding_price) && $bidding_price > $bid_price) {
                    $list[$key]['bid_method'] = '竞标';
                    $list[$key]['my_best_price'] = $bidding_price;
                } elseif (($bid_price || $bidding_price) && $bidding_price <= $bid_price) {
                    $list[$key]['bid_method'] = '投标';
                    $list[$key]['my_best_price'] = $bid_price;
                } else {
                    $list[$key]['bid_method'] = '--';
                    $list[$key]['my_best_price'] = '未出价';
                }

                if (($bid_price || $bidding_price) && $dealer_auctioned['status'] != '0' && $val['status'] == 13) {
                    $list[$key]['order_status'] = $order_status[$val['status']] . "(你未竞得)";
                }
                // elseif (($bid_price || $bidding_price) && $dealer_auctioned && $dealer_auctioned['status'] == 0 && $val['status'] == 13) {
                //     $list[$key]['order_status'] = $order_status[$val['status']]."(竞得)";
                // }
                else {
                    $list[$key]['order_status'] = $order_status[$val['status']];
                }
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }
}   
